我有以下格式的xml文件。
<?xml version="1.0" encoding="utf-8" ?>
<rss version="2.0" xmlns:g="http://base.google.com/ns/1.0">
<channel>
<title>Data feed Title</title>
<link>http://www.amarchitrakatha.com/in/</link>
<description>Data feed description.</description>
<item>ACKKALID0577
<g:id><![CDATA[ACKKALID0577]]></g:id>
<title><![CDATA[Kalidasa]]></title>
<link><![CDATA[http://www.amarchitrakatha.com/in/kalidasa?fee=19&fep=1147]]></link>
<g:price>70.00</g:price>
<g:sale_price></g:sale_price>
<description><![CDATA[Kalidas Owes His Fame To His Sanksrit Play Abhijnana-Shakuntala (Shakuntala Recognised By The Token Ring), The Long Epic Poem Kumara-Sambhava (Birth Of Kumara) And The Lyric Poem Meghaduta (Cloud Messenger). Kalidasa, Who Lived Some Time In The Middle Of The 4Th And Early 5Th Centuries A.D., Has Left No Account Of His Life. According To Popular Legend, He Owed His Ingenuity As Much To Goddess Kali’S Blessings As To His Own Talents.]]></description>
<g:product_type><![CDATA[Amar Chitra Katha > Visionaries > Singles]]></g:product_type>
<g:google_product_category><![CDATA[]]></g:google_product_category>
<g:image_link><![CDATA[http://www.amarchitrakatha.com/media/catalog/product/K/A/KALIDASA_0__5.jpg]]></g:image_link>
<g:condition>new</g:condition>
<g:availability>in stock</g:availability>
<g:shipping_weight>0.09 kg</g:shipping_weight>
<g:brand><![CDATA[]]></g:brand>
<g:mpn><![CDATA[ACKKALID0577]]></g:mpn>
<g:gtin><![CDATA[]]></g:gtin>
</item>
<item>ACKPRITH1413
<g:id><![CDATA[ACKPRITH1413]]></g:id>
<title><![CDATA[Prithviraj Chauhan]]></title>
<link><![CDATA[http://www.amarchitrakatha.com/in/prithviraj-chauhan?fee=19&fep=1150]]></link>
<g:price>70.00</g:price>
<g:sale_price></g:sale_price>
<description><![CDATA[Jaichand Was Furious! His Daughter Samyogita Had Eloped With Prithviraj Chauhan, The Warrior King Of Delhi. She, Like Many Others, Was Smitten By Tales Of His Daring, His Nobility And His Sense Of Honour. Though The Young Couple’S Happiness Was Doomed, Even In His Dying Hour, It Was The Brave Prithviraj Who Chose How His Life Should End.]]></description>
<g:product_type><![CDATA[Amar Chitra Katha > Brave Hearts Story > Singles]]></g:product_type>
<g:google_product_category><![CDATA[]]></g:google_product_category>
<g:image_link><![CDATA[http://www.amarchitrakatha.com/media/catalog/product/P/R/PRITHVIRAJ_CHAUHAN_0__4.jpg]]></g:image_link>
<g:condition>new</g:condition>
<g:availability>in stock</g:availability>
<g:shipping_weight>0.09 kg</g:shipping_weight>
<g:brand><![CDATA[]]></g:brand>
<g:mpn><![CDATA[ACKPRITH1413]]></g:mpn>
<g:gtin><![CDATA[]]></g:gtin>
</item>
</channel>
</rss>
使用simplexml_load_file
我无法获取项目数据。如何用PHP解析这种格式的XML?我想获取所有项目的数据,如g:id,title,link等。
if (file_exists('Sample.xml')) {
$xml = simplexml_load_file('Sample.xml');
echo '<pre>';
print_r($xml->channel);
echo '</pre>';
} else {
exit('Failed to open Sample.xml.');
}
得到结果
SimpleXMLElement Object
(
[title] => Data feed Title
[link] => http://www.amarchitrakatha.com/in/
[description] => Data feed description.
[item] => Array
(
[0] => ACKKALID0577
[1] => ACKPRITH1413
)
)
我必须将项目数据存储在特定变量中。
答案 0 :(得分:0)
我相信这个xml风格你可以使用标准的PHP xml解析器(即http://ca2.php.net/manual/en/class.simplexmlelement.php) 将它加载到您的变量中(即$ xml) 然后像这样读取这些节点:
$xml=simplexml_load_string($yourXMLFile)
$xml->children('g', true)->id;
(以上将提取&lt; g:id&gt;的所有实例)
没有测试我的结局。
答案 1 :(得分:0)
您可以使用PHP
DOMDocument
来完成任务。正如我在下面的代码中所做的那样。
<?php
ini_set('display_errors', 1);
$object= new DOMDocument();
$object->loadXML('<?xml version="1.0" encoding="utf-8" ?>
<rss version="2.0" xmlns:g="http://base.google.com/ns/1.0">
<channel>
<title>Data feed Title</title>
<link>http://www.amarchitrakatha.com/in/</link>
<description>Data feed description.</description>
<item>ACKKALID0577
<g:id><![CDATA[ACKKALID0577]]></g:id>
<title><![CDATA[Kalidasa]]></title>
<link><![CDATA[http://www.amarchitrakatha.com/in/kalidasa?fee=19&fep=1147]]></link>
<g:price>70.00</g:price>
<g:sale_price></g:sale_price>
<description><![CDATA[Kalidas Owes His Fame To His Sanksrit Play Abhijnana-Shakuntala (Shakuntala Recognised By The Token Ring), The Long Epic Poem Kumara-Sambhava (Birth Of Kumara) And The Lyric Poem Meghaduta (Cloud Messenger). Kalidasa, Who Lived Some Time In The Middle Of The 4Th And Early 5Th Centuries A.D., Has Left No Account Of His Life. According To Popular Legend, He Owed His Ingenuity As Much To Goddess Kali’S Blessings As To His Own Talents.]]></description>
<g:product_type><![CDATA[Amar Chitra Katha > Visionaries > Singles]]></g:product_type>
<g:google_product_category><![CDATA[]]></g:google_product_category>
<g:image_link><![CDATA[http://www.amarchitrakatha.com/media/catalog/product/K/A/KALIDASA_0__5.jpg]]></g:image_link>
<g:condition>new</g:condition>
<g:availability>in stock</g:availability>
<g:shipping_weight>0.09 kg</g:shipping_weight>
<g:brand><![CDATA[]]></g:brand>
<g:mpn><![CDATA[ACKKALID0577]]></g:mpn>
<g:gtin><![CDATA[]]></g:gtin>
</item>
<item>ACKPRITH1413
<g:id><![CDATA[ACKPRITH1413]]></g:id>
<title><![CDATA[Prithviraj Chauhan]]></title>
<link><![CDATA[http://www.amarchitrakatha.com/in/prithviraj-chauhan?fee=19&fep=1150]]></link>
<g:price>70.00</g:price>
<g:sale_price></g:sale_price>
<description><![CDATA[Jaichand Was Furious! His Daughter Samyogita Had Eloped With Prithviraj Chauhan, The Warrior King Of Delhi. She, Like Many Others, Was Smitten By Tales Of His Daring, His Nobility And His Sense Of Honour. Though The Young Couple’S Happiness Was Doomed, Even In His Dying Hour, It Was The Brave Prithviraj Who Chose How His Life Should End.]]></description>
<g:product_type><![CDATA[Amar Chitra Katha > Brave Hearts Story > Singles]]></g:product_type>
<g:google_product_category><![CDATA[]]></g:google_product_category>
<g:image_link><![CDATA[http://www.amarchitrakatha.com/media/catalog/product/P/R/PRITHVIRAJ_CHAUHAN_0__4.jpg]]></g:image_link>
<g:condition>new</g:condition>
<g:availability>in stock</g:availability>
<g:shipping_weight>0.09 kg</g:shipping_weight>
<g:brand><![CDATA[]]></g:brand>
<g:mpn><![CDATA[ACKPRITH1413]]></g:mpn>
<g:gtin><![CDATA[]]></g:gtin>
</item>
</channel>
</rss>');
$tagValues=array("g:id","title","link");
$result=array();
foreach($object->getElementsByTagName("item") as $item)
{
foreach($item->childNodes as $node)
{
if($node instanceof DOMElement)
{
if(in_array( $node->tagName,$tagValues))
{
$result[$node->tagName][]=$node->textContent;
}
}
}
}
print_r($result);
答案 2 :(得分:0)
试试这个,经过测试!
$xml = simplexml_load_file('http://example.com/rss.xml');
$namespaces = $xml->getNamespaces(true);
var_dump($namespaces);
输出:
array(1) {
["g"]=> string(29) "http://base.google.com/ns/1.0"
}
然后:
foreach ($xml->channel->item as $item)
{
// for php >= 5.2.0
// $id = (string) $item->children('g', TRUE)->id;
// for php < 5.2.0
// $id = (string) $item->children('http://base.google.com/ns/1.0')->id;
$id = (string) $item->children($namespaces["g"])->id; // for php < 5.2.0
$title = (string) $item->title;
$link = (string) $item->link;
var_dump($id, $title, $link);
echo "--\n";
}
输出:
string(12) "ACKKALID0577"
string(8) "Kalidasa"
string(58) "http://www.amarchitrakatha.com/in/kalidasa?fee=19&fep=1147"
--
string(12) "ACKPRITH1413"
string(18) "Prithviraj Chauhan"
string(68) "http://www.amarchitrakatha.com/in/prithviraj-chauhan?fee=19&fep=1150"
--
最后,例如为了将结果绑定到对象数组:
$xml = simplexml_load_file('http://example.com/rss.xml');
$namespaces = $xml->getNamespaces(true);
$itemArray = array();
foreach ($xml->channel->item as $item)
{
$obj = new stdClass();
$obj->id = (string) $item->children($namespaces["g"])->id; // for php < 5.2.0
$obj->title = (string) $item->title;
$obj->link = (string) $item->link;
$itemArray[] = $obj;
}
var_dump($itemArray);
输出:
array(2) {
[0]=> object(stdClass)#5 (3) {
["id"]=> string(12) "ACKKALID0577"
["title"]=> string(8) "Kalidasa"
["link"]=> string(58) "http://www.amarchitrakatha.com/in/kalidasa?fee=19&fep=1147"
}
[1]=> object(stdClass)#3 (3) {
["id"]=> string(12) "ACKPRITH1413"
["title"]=> string(18) "Prithviraj Chauhan"
["link"]=> string(68) "http://www.amarchitrakatha.com/in/prithviraj-chauhan?fee=19&fep=1150"
}
}
参考:http://php.net/manual/en/simplexmlelement.getnamespaces.php
参考:http://php.net/manual/en/simplexmlelement.children.php
参考:http://www.sitepoint.com/forums/showthread.php?606057-...
答案 3 :(得分:0)
您可以使用此
循环g:属性中的值$handle_object = $element->children('g', true);