我正在尝试使用PHP读取XML文件,但我无法获取数据,也无法找出原因。
XML结构:
<?xml version="1.0" encoding="UTF-8"?>
<FavoriteListResponse>
<metadata>
<offset>0</offset>
<limit>20</limit>
<total>1</total>
</metadata>
<favorites>
<favorites objectType="Product">
<id>5831fd52f9f0c0a80c88dafa</id>
<productId>511290702</productId>
<imageId>39d5e666255e351aa42fca3a9b939d04</imageId>
<product>
<id>511290702</id>
<name>Misha Black Cropped Leather Jacket With Asymmetrical Front Zipper</name>
<brandedName>Mackage Misha Black Cropped Leather Jacket With Asymmetrical Front Zipper</brandedName>
<unbrandedName>Misha Black Cropped Leather Jacket With Asymmetrical Front Zipper</unbrandedName>
<currency>USD</currency>
<price>620.0</price>
<priceLabel>Sold Out</priceLabel>
<inStock>false</inStock>
<stock>
<stock>
<size>
<name>M</name>
</size>
</stock>
<stock>
<size>
<name>XXS</name>
</size>
</stock>
</stock>
<retailer>
<id>596</id>
<name>Mackage</name>
<score>242</score>
</retailer>
<brand>
<id>1590</id>
<name>Mackage</name>
</brand>
<locale>en_US</locale>
<description>Misha by Mackage is a washed leather jacket for women crafted from genuine lambskin. Fitted silhouette cropped above the hips. Asymmetrical full-length zip closure. Dual zippered front pockets. Nickel hardware. Available in black and sand. Fits true to size. Fitted silhouette. Washed genuine lambskin leather jacket. Cropped above the hips. Padded shoulder yoke. Hem yoke with stitching at the back bottom. Asymmetrical full-length two-way zip closure. Nickel hardware. Coat length from shoulder to hem : 17inches / 43.2cm Model is 5'11/180 cm and wearing a size XS. Shell: 100% Lambskin. Lining (Woven): 100% Polyester.</description>
<clickUrl>https://api.shopstyle.com/action/apiVisitRetailer?id=511290702&pid=uid5044-35323862-95</clickUrl>
<image>
<sizes>
<Small>
<sizeName>Small</sizeName>
<url>https://img.shopstyle-cdn.com/pim/39/d5/39d5e666255e351aa42fca3a9b939d04_small.jpg</url>
<width>32</width>
<height>40</height>
<actualWidth>11</actualWidth>
<actualHeight>40</actualHeight>
</Small>
<XLarge>
<sizeName>XLarge</sizeName>
<url>https://img.shopstyle-cdn.com/pim/39/d5/39d5e666255e351aa42fca3a9b939d04_xlarge.jpg</url>
<width>328</width>
<height>410</height>
<actualWidth>111</actualWidth>
<actualHeight>410</actualHeight>
</XLarge>
<Medium>
<sizeName>Medium</sizeName>
<url>https://img.shopstyle-cdn.com/sim/39/d5/39d5e666255e351aa42fca3a9b939d04_medium/mackage-misha-black-cropped-leather-jacket-with-asymmetrical-front-zipper.jpg</url>
<width>112</width>
<height>140</height>
<actualWidth>38</actualWidth>
<actualHeight>140</actualHeight>
</Medium>
<Large>
<sizeName>Large</sizeName>
<url>https://img.shopstyle-cdn.com/sim/39/d5/39d5e666255e351aa42fca3a9b939d04/mackage-misha-black-cropped-leather-jacket-with-asymmetrical-front-zipper.jpg</url>
<width>164</width>
<height>205</height>
<actualWidth>55</actualWidth>
<actualHeight>205</actualHeight>
</Large>
<IPhoneSmall>
<sizeName>IPhoneSmall</sizeName>
<url>https://img.shopstyle-cdn.com/mim/39/d5/39d5e666255e351aa42fca3a9b939d04_small.jpg</url>
<width>100</width>
<height>125</height>
<actualWidth>34</actualWidth>
<actualHeight>125</actualHeight>
</IPhoneSmall>
<Best>
<sizeName>Best</sizeName>
<url>https://img.shopstyle-cdn.com/pim/39/d5/39d5e666255e351aa42fca3a9b939d04_best.jpg</url>
<width>720</width>
<height>900</height>
<actualWidth>243</actualWidth>
<actualHeight>900</actualHeight>
</Best>
<Original>
<sizeName>Original</sizeName>
<url>https://img.shopstyle-cdn.com/pim/39/d5/39d5e666255e351aa42fca3a9b939d04_best.jpg</url>
<actualWidth>243</actualWidth>
<actualHeight>900</actualHeight>
</Original>
<IPhone>
<sizeName>IPhone</sizeName>
<url>https://img.shopstyle-cdn.com/mim/39/d5/39d5e666255e351aa42fca3a9b939d04.jpg</url>
<width>288</width>
<height>360</height>
<actualWidth>97</actualWidth>
<actualHeight>360</actualHeight>
</IPhone>
</sizes>
<id>39d5e666255e351aa42fca3a9b939d04</id>
</image>
<alternateImages>
<alternateImages>
<sizes>
<Small>
<sizeName>Small</sizeName>
<url>https://img.shopstyle-cdn.com/pim/39/d5/39d5e666255e351aa42fca3a9b939d04_small.jpg</url>
<width>32</width>
<height>40</height>
<actualWidth>11</actualWidth>
<actualHeight>40</actualHeight>
</Small>
<XLarge>
<sizeName>XLarge</sizeName>
<url>https://img.shopstyle-cdn.com/pim/39/d5/39d5e666255e351aa42fca3a9b939d04_xlarge.jpg</url>
<width>328</width>
<height>410</height>
<actualWidth>111</actualWidth>
<actualHeight>410</actualHeight>
</XLarge>
<Medium>
<sizeName>Medium</sizeName>
<url>https://img.shopstyle-cdn.com/sim/39/d5/39d5e666255e351aa42fca3a9b939d04_medium/mackage-misha-black-cropped-leather-jacket-with-asymmetrical-front-zipper.jpg</url>
<width>112</width>
<height>140</height>
<actualWidth>38</actualWidth>
<actualHeight>140</actualHeight>
</Medium>
<Large>
<sizeName>Large</sizeName>
<url>https://img.shopstyle-cdn.com/sim/39/d5/39d5e666255e351aa42fca3a9b939d04/mackage-misha-black-cropped-leather-jacket-with-asymmetrical-front-zipper.jpg</url>
<width>164</width>
<height>205</height>
<actualWidth>55</actualWidth>
<actualHeight>205</actualHeight>
</Large>
<IPhoneSmall>
<sizeName>IPhoneSmall</sizeName>
<url>https://img.shopstyle-cdn.com/mim/39/d5/39d5e666255e351aa42fca3a9b939d04_small.jpg</url>
<width>100</width>
<height>125</height>
<actualWidth>34</actualWidth>
<actualHeight>125</actualHeight>
</IPhoneSmall>
<Best>
<sizeName>Best</sizeName>
<url>https://img.shopstyle-cdn.com/pim/39/d5/39d5e666255e351aa42fca3a9b939d04_best.jpg</url>
<width>720</width>
<height>900</height>
<actualWidth>243</actualWidth>
<actualHeight>900</actualHeight>
</Best>
<Original>
<sizeName>Original</sizeName>
<url>https://img.shopstyle-cdn.com/pim/39/d5/39d5e666255e351aa42fca3a9b939d04_best.jpg</url>
<actualWidth>243</actualWidth>
<actualHeight>900</actualHeight>
</Original>
<IPhone>
<sizeName>IPhone</sizeName>
<url>https://img.shopstyle-cdn.com/mim/39/d5/39d5e666255e351aa42fca3a9b939d04.jpg</url>
<width>288</width>
<height>360</height>
<actualWidth>97</actualWidth>
<actualHeight>360</actualHeight>
</IPhone>
</sizes>
<id>39d5e666255e351aa42fca3a9b939d04</id>
</alternateImages>
<alternateImages>
<sizes>
<Small>
<sizeName>Small</sizeName>
<url>https://img.shopstyle-cdn.com/pim/56/c4/56c4c2a990fb868e7a27a57381330244_small.jpg</url>
<width>32</width>
<height>40</height>
<actualWidth>12</actualWidth>
<actualHeight>40</actualHeight>
</Small>
<XLarge>
<sizeName>XLarge</sizeName>
<url>https://img.shopstyle-cdn.com/pim/56/c4/56c4c2a990fb868e7a27a57381330244_xlarge.jpg</url>
<width>328</width>
<height>410</height>
<actualWidth>124</actualWidth>
<actualHeight>410</actualHeight>
</XLarge>
<Medium>
<sizeName>Medium</sizeName>
<url>https://img.shopstyle-cdn.com/sim/56/c4/56c4c2a990fb868e7a27a57381330244_medium/mackage-misha-black-cropped-leather-jacket-with-asymmetrical-front-zipper.jpg</url>
<width>112</width>
<height>140</height>
<actualWidth>42</actualWidth>
<actualHeight>140</actualHeight>
</Medium>
<Large>
<sizeName>Large</sizeName>
<url>https://img.shopstyle-cdn.com/sim/56/c4/56c4c2a990fb868e7a27a57381330244/mackage-misha-black-cropped-leather-jacket-with-asymmetrical-front-zipper.jpg</url>
<width>164</width>
<height>205</height>
<actualWidth>62</actualWidth>
<actualHeight>205</actualHeight>
</Large>
<IPhoneSmall>
<sizeName>IPhoneSmall</sizeName>
<url>https://img.shopstyle-cdn.com/mim/56/c4/56c4c2a990fb868e7a27a57381330244_small.jpg</url>
<width>100</width>
<height>125</height>
<actualWidth>38</actualWidth>
<actualHeight>125</actualHeight>
</IPhoneSmall>
<Best>
<sizeName>Best</sizeName>
<url>https://img.shopstyle-cdn.com/pim/56/c4/56c4c2a990fb868e7a27a57381330244_best.jpg</url>
<width>720</width>
<height>900</height>
<actualWidth>273</actualWidth>
<actualHeight>900</actualHeight>
</Best>
<Original>
<sizeName>Original</sizeName>
<url>https://img.shopstyle-cdn.com/pim/56/c4/56c4c2a990fb868e7a27a57381330244_best.jpg</url>
<actualWidth>273</actualWidth>
<actualHeight>900</actualHeight>
</Original>
<IPhone>
<sizeName>IPhone</sizeName>
<url>https://img.shopstyle-cdn.com/mim/56/c4/56c4c2a990fb868e7a27a57381330244.jpg</url>
<width>288</width>
<height>360</height>
<actualWidth>109</actualWidth>
<actualHeight>360</actualHeight>
</IPhone>
</sizes>
<id>56c4c2a990fb868e7a27a57381330244</id>
</alternateImages>
<alternateImages>
<sizes>
<Small>
<sizeName>Small</sizeName>
<url>https://img.shopstyle-cdn.com/pim/a9/b2/a9b2551df3fe88d30f596ab83a1e764c_small.jpg</url>
<width>32</width>
<height>40</height>
<actualWidth>10</actualWidth>
<actualHeight>40</actualHeight>
</Small>
<XLarge>
<sizeName>XLarge</sizeName>
<url>https://img.shopstyle-cdn.com/pim/a9/b2/a9b2551df3fe88d30f596ab83a1e764c_xlarge.jpg</url>
<width>328</width>
<height>410</height>
<actualWidth>108</actualWidth>
<actualHeight>410</actualHeight>
</XLarge>
<Medium>
<sizeName>Medium</sizeName>
<url>https://img.shopstyle-cdn.com/sim/a9/b2/a9b2551df3fe88d30f596ab83a1e764c_medium/mackage-misha-black-cropped-leather-jacket-with-asymmetrical-front-zipper.jpg</url>
<width>112</width>
<height>140</height>
<actualWidth>37</actualWidth>
<actualHeight>140</actualHeight>
</Medium>
<Large>
<sizeName>Large</sizeName>
<url>https://img.shopstyle-cdn.com/sim/a9/b2/a9b2551df3fe88d30f596ab83a1e764c/mackage-misha-black-cropped-leather-jacket-with-asymmetrical-front-zipper.jpg</url>
<width>164</width>
<height>205</height>
<actualWidth>54</actualWidth>
<actualHeight>205</actualHeight>
</Large>
<IPhoneSmall>
<sizeName>IPhoneSmall</sizeName>
<url>https://img.shopstyle-cdn.com/mim/a9/b2/a9b2551df3fe88d30f596ab83a1e764c_small.jpg</url>
<width>100</width>
<height>125</height>
<actualWidth>33</actualWidth>
<actualHeight>125</actualHeight>
</IPhoneSmall>
<Best>
<sizeName>Best</sizeName>
<url>https://img.shopstyle-cdn.com/pim/a9/b2/a9b2551df3fe88d30f596ab83a1e764c_best.jpg</url>
<width>720</width>
<height>900</height>
<actualWidth>236</actualWidth>
<actualHeight>900</actualHeight>
</Best>
<Original>
<sizeName>Original</sizeName>
<url>https://img.shopstyle-cdn.com/pim/a9/b2/a9b2551df3fe88d30f596ab83a1e764c_best.jpg</url>
<actualWidth>236</actualWidth>
<actualHeight>900</actualHeight>
</Original>
<IPhone>
<sizeName>IPhone</sizeName>
<url>https://img.shopstyle-cdn.com/mim/a9/b2/a9b2551df3fe88d30f596ab83a1e764c.jpg</url>
<width>288</width>
<height>360</height>
<actualWidth>94</actualWidth>
<actualHeight>360</actualHeight>
</IPhone>
</sizes>
<id>a9b2551df3fe88d30f596ab83a1e764c</id>
</alternateImages>
<alternateImages>
<sizes>
<Small>
<sizeName>Small</sizeName>
<url>https://img.shopstyle-cdn.com/pim/b8/2a/b82a3ff82915a288cedb59386eb74c4c_small.jpg</url>
<width>32</width>
<height>40</height>
<actualWidth>11</actualWidth>
<actualHeight>40</actualHeight>
</Small>
<XLarge>
<sizeName>XLarge</sizeName>
<url>https://img.shopstyle-cdn.com/pim/b8/2a/b82a3ff82915a288cedb59386eb74c4c_xlarge.jpg</url>
<width>328</width>
<height>410</height>
<actualWidth>114</actualWidth>
<actualHeight>410</actualHeight>
</XLarge>
<Medium>
<sizeName>Medium</sizeName>
<url>https://img.shopstyle-cdn.com/sim/b8/2a/b82a3ff82915a288cedb59386eb74c4c_medium/mackage-misha-black-cropped-leather-jacket-with-asymmetrical-front-zipper.jpg</url>
<width>112</width>
<height>140</height>
<actualWidth>39</actualWidth>
<actualHeight>140</actualHeight>
</Medium>
<Large>
<sizeName>Large</sizeName>
<url>https://img.shopstyle-cdn.com/sim/b8/2a/b82a3ff82915a288cedb59386eb74c4c/mackage-misha-black-cropped-leather-jacket-with-asymmetrical-front-zipper.jpg</url>
<width>164</width>
<height>205</height>
<actualWidth>57</actualWidth>
<actualHeight>205</actualHeight>
</Large>
<IPhoneSmall>
<sizeName>IPhoneSmall</sizeName>
<url>https://img.shopstyle-cdn.com/mim/b8/2a/b82a3ff82915a288cedb59386eb74c4c_small.jpg</url>
<width>100</width>
<height>125</height>
<actualWidth>35</actualWidth>
<actualHeight>125</actualHeight>
</IPhoneSmall>
<Best>
<sizeName>Best</sizeName>
<url>https://img.shopstyle-cdn.com/pim/b8/2a/b82a3ff82915a288cedb59386eb74c4c_best.jpg</url>
<width>720</width>
<height>900</height>
<actualWidth>250</actualWidth>
<actualHeight>900</actualHeight>
</Best>
<Original>
<sizeName>Original</sizeName>
<url>https://img.shopstyle-cdn.com/pim/b8/2a/b82a3ff82915a288cedb59386eb74c4c_best.jpg</url>
<actualWidth>250</actualWidth>
<actualHeight>900</actualHeight>
</Original>
<IPhone>
<sizeName>IPhone</sizeName>
<url>https://img.shopstyle-cdn.com/mim/b8/2a/b82a3ff82915a288cedb59386eb74c4c.jpg</url>
<width>288</width>
<height>360</height>
<actualWidth>100</actualWidth>
<actualHeight>360</actualHeight>
</IPhone>
</sizes>
<id>b82a3ff82915a288cedb59386eb74c4c</id>
</alternateImages>
<alternateImages>
<sizes>
<Small>
<sizeName>Small</sizeName>
<url>https://img.shopstyle-cdn.com/pim/e4/9d/e49db198c964ac5c339b2924687c633c_small.jpg</url>
<width>32</width>
<height>40</height>
<actualWidth>32</actualWidth>
<actualHeight>40</actualHeight>
</Small>
<XLarge>
<sizeName>XLarge</sizeName>
<url>https://img.shopstyle-cdn.com/pim/e4/9d/e49db198c964ac5c339b2924687c633c_xlarge.jpg</url>
<width>328</width>
<height>410</height>
<actualWidth>328</actualWidth>
<actualHeight>410</actualHeight>
</XLarge>
<Medium>
<sizeName>Medium</sizeName>
<url>https://img.shopstyle-cdn.com/sim/e4/9d/e49db198c964ac5c339b2924687c633c_medium/mackage-misha-black-cropped-leather-jacket-with-asymmetrical-front-zipper.jpg</url>
<width>112</width>
<height>140</height>
<actualWidth>112</actualWidth>
<actualHeight>140</actualHeight>
</Medium>
<Large>
<sizeName>Large</sizeName>
<url>https://img.shopstyle-cdn.com/sim/e4/9d/e49db198c964ac5c339b2924687c633c/mackage-misha-black-cropped-leather-jacket-with-asymmetrical-front-zipper.jpg</url>
<width>164</width>
<height>205</height>
<actualWidth>164</actualWidth>
<actualHeight>205</actualHeight>
</Large>
<IPhoneSmall>
<sizeName>IPhoneSmall</sizeName>
<url>https://img.shopstyle-cdn.com/mim/e4/9d/e49db198c964ac5c339b2924687c633c_small.jpg</url>
<width>100</width>
<height>125</height>
<actualWidth>100</actualWidth>
<actualHeight>125</actualHeight>
</IPhoneSmall>
<Best>
<sizeName>Best</sizeName>
<url>https://img.shopstyle-cdn.com/pim/e4/9d/e49db198c964ac5c339b2924687c633c_best.jpg</url>
<width>720</width>
<height>900</height>
<actualWidth>720</actualWidth>
<actualHeight>900</actualHeight>
</Best>
<Original>
<sizeName>Original</sizeName>
<url>https://img.shopstyle-cdn.com/pim/e4/9d/e49db198c964ac5c339b2924687c633c_best.jpg</url>
</Original>
<IPhone>
<sizeName>IPhone</sizeName>
<url>https://img.shopstyle-cdn.com/mim/e4/9d/e49db198c964ac5c339b2924687c633c.jpg</url>
<width>288</width>
<height>360</height>
<actualWidth>288</actualWidth>
<actualHeight>360</actualHeight>
</IPhone>
</sizes>
<id>e49db198c964ac5c339b2924687c633c</id>
</alternateImages>
</alternateImages>
<extractDate>2016-02-16</extractDate>
<lastModified>2017-02-05</lastModified>
<seeMoreLabel>Mackage Leather Jackets</seeMoreLabel>
<preOwned>false</preOwned>
<rental>false</rental>
<colors />
<sizes>
<size>
<name>XXS</name>
<canonicalSize>
<id>79</id>
<name>XXS (0)</name>
</canonicalSize>
</size>
<size>
<name>M</name>
<canonicalSize>
<id>85</id>
<name>M (8-10)</name>
</canonicalSize>
</size>
</sizes>
<categories>
<category>
<id>leather-jackets</id>
<name>Leather Jackets</name>
<shortName>Leather</shortName>
<fullName>Women's Leather Jackets</fullName>
<localizedId>leather-jackets</localizedId>
</category>
</categories>
<badges />
</product>
<date>
<date>2016-11-20</date>
<timestamp>1479671122</timestamp>
<friendly>Nov 20, 2016</friendly>
</date>
<objectId>511290702</objectId>
</favorites>
</favorites>
</FavoriteListResponse>
我无法理解。希望有人愿意帮忙:)。
答案 0 :(得分:0)
我通常将此用于我的工作目的,你可以尝试使用它,
/**
* Class xml2array
*/
class xml2array {
var $arrOutput = array();
var $resParser;
var $strXmlData;
function parse($strInputXML) {
$this->resParser = xml_parser_create ();
xml_set_object($this->resParser,$this);
xml_set_element_handler($this->resParser, "tagOpen", "tagClosed");
xml_set_character_data_handler($this->resParser, "tagData");
$this->strXmlData = xml_parse($this->resParser,$strInputXML );
if(!$this->strXmlData) {
die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($this->resParser)),
xml_get_current_line_number($this->resParser)));
}
xml_parser_free($this->resParser);
return $this->arrOutput;
}
function tagOpen($parser, $name, $attrs) {
$tag=array("name"=>$name,"attrs"=>$attrs);
array_push($this->arrOutput,$tag);
}
function tagData($parser, $tagData) {
if(trim($tagData)) {
if(isset($this->arrOutput[count($this->arrOutput)-1]['tagData'])) {
$this->arrOutput[count($this->arrOutput)-1]['tagData'] .= $tagData;
}
else {
$this->arrOutput[count($this->arrOutput)-1]['tagData'] = $tagData;
}
}
}
function tagClosed($parser, $name) {
$this->arrOutput[count($this->arrOutput)-2]['children'][] = $this->arrOutput[count($this->arrOutput)-1];
array_pop($this->arrOutput);
}
}
$xml_file = file_get_contents('yourxmlfilename.xml');
$objXML = new xml2array();
$arrOutput = $objXML->parse($xml_file);
print '<pre>';
print_r($arrOutput); //print it out, or do whatever!
print '</pre>';
答案 1 :(得分:0)
function actionFindDataByXMLTag( $result, $xml_tag, $xml_end_tag, $count = null ) {
$next_string = '';
$result_array = array();
$stop = 500;//only 500 items values will be selected
$i = 0;
while($i < $stop) {
if($next_string == '') {
$from = $result;
} else {
$from = $next_string;
}
$position = strpos($from, $xml_tag) + strlen($xml_tag);
$start = substr($from, $position);
$end = strpos($start, $xml_end_tag);
$terr_result = substr($start, 0, $end);
if($terr_result == '') {
break;
} else {
array_push($result_array, $terr_result);
}
$next_string = substr($start, $end + strlen($xml_end_tag));
//var_dump( $next_string );
$i++;
}
return $result_array;
}
答案 2 :(得分:0)
SimpleXML是获取此数据的简便方法。此代码假定数据位于文件中,但只需将class Handle{
public:
Handle() {
QString path = QCoreApplication::applicationDirPath();
path.append("/foo.log");
_file.setFileName(path);
_stream.setDevice(&_file);
}
QFile _file;
QTextStream _stream;
};
template <class T>
Handle& operator<< (Handle& out, T msg) {
out._file.open(QIODevice::WriteOnly | QIODevice::Append);
out._stream << msg;
return out;
}
更改为simplexml_load_file
并在其中传递XML。
simplexml_load_string
这只显示部分数据,但我希望它提供了如何使用XML结构的想法。使用<?php
error_reporting ( E_ALL );
ini_set ( 'display_errors', 1 );
$xml = simplexml_load_file('t1.xml');
foreach( $xml->favorites->favorites->product as $product ){
echo "Product name=".(string)$product->name.PHP_EOL;
echo "Retailer=".(string)$product->retailer->name.PHP_EOL;
foreach ( $product->sizes->size as $size ) {
echo "Size:".(string)$size->name.PHP_EOL;
}
echo "Small image=".(string)$product->image->sizes->Small->url;
}
为$ xml值提供了favorites元素。因此$xml->favorites
会浏览每个产品,然后$xml->favorites->favorites->product
会为您提供名称元素。从每个产品中,此代码使用$product->name
来查看每个大小,但对于任何数据项都可以执行相同的操作。
此输出(使用您的示例XML):
$product->sizes->size