[英]I want to echo out the categories from this XML-file
我遇到的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循环结束
正如您在XML代码中看到的那样,我的类别字段称为CATEGORYNAME,但我不知道如何输出此数组。 回显所有其他字段很容易,但是当我将类别作为子字段放入时,会出现错误...
我已经尝试过两次了...
您可以将循环放入循环中,以便从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摘自您的代码。
获得类别名称的简化代码:试试这个
$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/>";
}
}
OUTPUT
奥托马蒂克 - 马斯金
Borrmaskin
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.