我有以下XML文件,这个文件相当大,我无法让simplexml打开并读取该文件,所以我在php中尝试XMLReader,但没有成功。
<?xml version="1.0" encoding="ISO-8859-1"?>
<products>
<last_updated>2009-11-30 13:52:40</last_updated>
<product>
<element_1>foo</element_1>
<element_2>foo</element_2>
<element_3>foo</element_3>
<element_4>foo</element_4>
</product>
<product>
<element_1>bar</element_1>
<element_2>bar</element_2>
<element_3>bar</element_3>
<element_4>bar</element_4>
</product>
</products>
不幸的是,我还没有找到一个很好的PHP教程,我很想看看如何将每个元素内容存储到数据库中。
发布于 2012-10-06 01:06:24
对于使用属性格式化的xml ...
data.xml:
<building_data>
<building address="some address" lat="28.902914" lng="-71.007235" />
<building address="some address" lat="48.892342" lng="-75.0423423" />
<building address="some address" lat="58.929753" lng="-79.1236987" />
</building_data>
php代码:
$reader = new XMLReader();
if (!$reader->open("data.xml")) {
die("Failed to open 'data.xml'");
}
while($reader->read()) {
if ($reader->nodeType == XMLReader::ELEMENT && $reader->name == 'building') {
$address = $reader->getAttribute('address');
$latitude = $reader->getAttribute('lat');
$longitude = $reader->getAttribute('lng');
}
$reader->close();
发布于 2014-06-20 14:07:38
公认的答案给了我一个很好的开始,但带来了比我希望的更多的类和更多的处理;所以这是我的解释:
$xml_reader = new XMLReader;
$xml_reader->open($feed_url);
// move the pointer to the first product
while ($xml_reader->read() && $xml_reader->name != 'product');
// loop through the products
while ($xml_reader->name == 'product')
{
// load the current xml element into simplexml and we’re off and running!
$xml = simplexml_load_string($xml_reader->readOuterXML());
// now you can use your simpleXML object ($xml).
echo $xml->element_1;
// move the pointer to the next product
$xml_reader->next('product');
}
// don’t forget to close the file
$xml_reader->close();
发布于 2014-10-16 02:02:44
我的大部分XML解析工作都花在从大量XML (Amazon MWS)中提取有用信息上。因此,我的答案假设您只需要特定的信息,并且您知道它的位置。
我发现使用XMLReader最简单的方法是知道我想要从哪些标签中提取信息并使用它们。如果您知道XML的结构,并且它有许多独特的标记,我发现使用第一种情况比较容易。案例2和案例3只是为了向您展示如何对更复杂的标记执行此操作。这是非常快的;我在What is the fastest XML parser in PHP?上讨论了速度
在进行这样的基于标记的解析时,最重要的是要记住使用if ($myXML->nodeType == XMLReader::ELEMENT) {...
-它会检查以确保我们只处理开始节点,而不是空格或关闭节点或其他任何东西。
function parseMyXML ($xml) { //pass in an XML string
$myXML = new XMLReader();
$myXML->xml($xml);
while ($myXML->read()) { //start reading.
if ($myXML->nodeType == XMLReader::ELEMENT) { //only opening tags.
$tag = $myXML->name; //make $tag contain the name of the tag
switch ($tag) {
case 'Tag1': //this tag contains no child elements, only the content we need. And it's unique.
$variable = $myXML->readInnerXML(); //now variable contains the contents of tag1
break;
case 'Tag2': //this tag contains child elements, of which we only want one.
while($myXML->read()) { //so we tell it to keep reading
if ($myXML->nodeType == XMLReader::ELEMENT && $myXML->name === 'Amount') { // and when it finds the amount tag...
$variable2 = $myXML->readInnerXML(); //...put it in $variable2.
break;
}
}
break;
case 'Tag3': //tag3 also has children, which are not unique, but we need two of the children this time.
while($myXML->read()) {
if ($myXML->nodeType == XMLReader::ELEMENT && $myXML->name === 'Amount') {
$variable3 = $myXML->readInnerXML();
break;
} else if ($myXML->nodeType == XMLReader::ELEMENT && $myXML->name === 'Currency') {
$variable4 = $myXML->readInnerXML();
break;
}
}
break;
}
}
}
$myXML->close();
}
https://stackoverflow.com/questions/1835177
复制相似问题