我想回声一下这个XML文件中的类别

时间:2017-02-28 08:34:45

标签: php xml simplexml

我遇到了一些与我挣扎的XML文件有关的问题。

当我执行PRINT_R时,输出将为:

SimpleXMLElement Object ( ) SimpleXMLElement Object ( ) SimpleXMLElement Object ( [CATEGORYNAME] => Array ( [0] => Automatik-maskin [1] => Borrmaskin ) ) SimpleXMLElement Object ( )

XML:

<CATALOG>
<ENGINE>
    <TITLE>Another product</TITLE>
    <ARTNR>75</ARTNR>
    <TEXT>This is another awesome product</TEXT>
    <CATEGORIES></CATEGORIES>
</ENGINE>
<ENGINE>
    <TITLE>Borrmaskin</TITLE>
    <ARTNR>3530</ARTNR>
    <TEXT>This is awesome</TEXT>

    <QTY>10</QTY>
    <CATEGORIES>
            <CATEGORYNAME>Automatik-maskin</CATEGORYNAME>
            <CATEGORYNAME>Borrmaskin</CATEGORYNAME>
    </CATEGORIES>
    <PRICE>10.90</PRICE>
</ENGINE>   

    $xml = simplexml_load_file("../../xml/xml-stad.xml") or die("Could not find the file...");

//Loop for the insert of new product or update an existing.
foreach ($xml as $i){

//Converting the XML-tags to variables:
$type       ='product'; //Define the wordpress post type
$title      = (string)$i->TITLE; //Product-title
$artnumber  = (string)$i->ARTNR; //Product article number
$text       = (string)$i->TEXT; //Product description
$qty        = (string)$i->QTY; //Numbers of products each package
$packqty    = (string)$i->PACKQTY; //Numbers of packages each pallet
$width      = (string)$i->WIDTH; //Width of product
$height     = (string)$i->HEIGHT; //Height of product 
$categories = $i->CATEGORIES; //Categories of the product (this can be multiple)
$price      = (string)$i->PRICE; //Price of product
$campaign   = (string)$i->CAMPAIGN; //If a campaign is present this is declared here,
$image      = (string)$i->IMAGE; //Image source of a product
$branch     = (string)$i->BRANCH; //Branch of product
$date       = date('Y-m-t') .' ' . date('H:i:s');

//Arrays for wp_posts-table
$data_post_table = array(
    'post_type'     => $type,
    'post_title'    => $title,
    'post_status'   => 'publish',
    'post_date'     => $date,
    'post_date_gmt' => $date,
    'post_author'   => '1',
);

    global $wpdb;

    //Check if there is a Post_id for the artnr
    $sql = $wpdb->get_row("SELECT post_id FROM wp_postmeta WHERE meta_key = 'artnr' AND meta_value = '".$artnumber."' ");
    if($wpdb->num_rows > 0) {
        //Declaring variables for the table update.
        $postId = $sql->post_id;

        //Update affected rows
        update_post_meta($postId, 'text', $text);
        update_post_meta($postId, 'qty', $qty);
        update_post_meta($postId, 'packqty', $packqty);
        update_post_meta($postId, 'width', $width);
        update_post_meta($postId, 'height', $height);
        //update_post_meta($postId, 'categories', $categories);
        update_post_meta($postId, 'price', $price);
        update_post_meta($postId, 'campaign', $campaign);
        update_post_meta($postId, 'image', $image);
        update_post_meta($postId, 'branch', $branch);
        print_r($categories);
    } else {

        //If product does not exist in wp_post table.
        //Insert into Database ->  wp_post (the table)
        $wpdb->insert('wp_posts', $data_post_table);

        //Get the post-id of inserted row
        $post_id = $wpdb->insert_id;    

        //Declaring variables for creating a new row in wp_postmeta (the table) with the key preferences for the product.
            $data_post_meta = array(
                'post_id' => $post_id,
                'meta_key' => 'artnr',
                'meta_value' => $artnumber
            );
        $wpdb->insert('wp_postmeta', $data_post_meta);

        //Create new rows based on the generated post_id.
        add_post_meta($post_id, 'text', $text);
        add_post_meta($post_id, 'qty', $qty);
        add_post_meta($post_id, 'packqty', $packqty);
        add_post_meta($post_id, 'width', $width);
        add_post_meta($post_id, 'height', $height);
        add_post_meta($post_id, 'categories', $categories);
        add_post_meta($post_id, 'price', $price);
        add_post_meta($post_id, 'campaign', $campaign);
        add_post_meta($post_id, 'image', $image);
        add_post_meta($post_id, 'branch', $branch);

} //End of IF-statement

} // foreach-loop结束

正如您在XML代码中看到的,我的类别字段名为CATEGORYNAME,但我无法弄清楚如何输出此数组。很容易回应所有其他字段但是当我将类别作为子字段放入时会出现错误...

我曾经尝试了几次......

2 个答案:

答案 0 :(得分:0)

您可以在循环中放置一个循环,以便从XML中获取所有类别。

$xml = simplexml_load_string($xml);
foreach ($xml as $i){
    if(count($i->CATEGORIES->CATEGORYNAME) > 0)
        foreach ($i->CATEGORIES->CATEGORYNAME as $category_name) 
            var_dump((string) $category_name);
}

第一个foreach取自你的代码。

答案 1 :(得分:0)

获取类别名称的简化代码:试试这个

$xml = '<?xml version="1.0" encoding="UTF-8"?><CATALOG>
<ENGINE>
    <TITLE>Another product</TITLE>
    <ARTNR>75</ARTNR>
    <TEXT>This is another awesome product</TEXT>
    <CATEGORIES></CATEGORIES>
</ENGINE>
<ENGINE>
    <TITLE>Borrmaskin</TITLE>
    <ARTNR>3530</ARTNR>
    <TEXT>This is awesome</TEXT>

    <QTY>10</QTY>
    <CATEGORIES>
            <CATEGORYNAME>Automatik-maskin</CATEGORYNAME>
            <CATEGORYNAME>Borrmaskin</CATEGORYNAME>
    </CATEGORIES>
    <PRICE>10.90</PRICE>
</ENGINE></CATALOG>';



 $xmlcont = new SimpleXMLElement($xml);

 foreach ($xmlcont as $value) {
    $val = $value->CATEGORIES->CATEGORYNAME;
    foreach ($val as $key) {
        echo $key."<br/>";
    }

 }
  

输出

奥托马蒂克-马斯

Borrmaskin