我有以下 XML 文件,该文件相当大,我无法使用 simplexml 打开和读取该文件,所以我尝试使用 XMLReader,但在 php 中没有成功
<?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 的好的教程,并且很想看看如何将每个元素内容存储在数据库中。
这一切都取决于工作单元有多大,但我猜你正在尝试连续处理每个
<product/>
节点。
为此,最简单的方法是使用 XMLReader 获取每个节点,然后使用 SimpleXML 访问它们。这样,您可以保持较低的内存使用量,因为您一次处理一个节点,并且仍然可以利用 SimpleXML 的易用性。例如:
$z = new XMLReader;
$z->open('data.xml');
$doc = new DOMDocument;
// move to the first <product /> node
while ($z->read() && $z->name !== 'product');
// now that we're at the right depth, hop to the next <product/> until the end of the tree
while ($z->name === 'product')
{
// either one should work
//$node = new SimpleXMLElement($z->readOuterXML());
$node = simplexml_import_dom($doc->importNode($z->expand(), true));
// now you can use $node without going insane about parsing
var_dump($node->element_1);
// go to next <product />
$z->next('product');
}
快速概述不同方法的优缺点:
仅限 XMLReader
优点:速度快,占用内存小
缺点:编写和调试非常困难,需要大量用户态代码来完成任何有用的事情。用户态代码速度慢且容易出错。另外,它还让您需要维护更多行代码
XMLReader + SimpleXML
优点:不占用太多内存(仅处理一个节点所需的内存),而且 SimpleXML 顾名思义,非常易于使用。
缺点:为每个节点创建 SimpleXMLElement 对象不是很快。您确实必须对其进行基准测试才能了解这对您来说是否是一个问题。不过,即使是一台普通的机器也能够每秒处理一千个节点。
XMLReader + DOM
优点:使用与 SimpleXML 一样多的内存,并且 XMLReader::expand() 比创建新的 SimpleXMLElement 更快。我希望可以使用
simplexml_import_dom()
但在这种情况下似乎不起作用缺点:DOM 使用起来很烦人。它介于 XMLReader 和 SimpleXML 之间。不像 XMLReader 那样复杂和尴尬,但与使用 SimpleXML 相差甚远。
我的建议:使用 SimpleXML 编写原型,看看它是否适合您。如果性能至关重要,请尝试 DOM。尽可能远离 XMLReader。请记住,您编写的代码越多,引入错误或引入性能回归的可能性就越高。
对于使用属性格式化的 xml...
数据.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();
我的 XML 解析生涯的大部分时间都花在从大量 XML (Amazon MWS) 中提取有用信息。因此,我的回答假设您只需要特定信息并且您知道它位于哪里。
我发现使用 XMLReader 最简单的方法是知道我想要从哪些标签中获取信息并使用它们。如果您了解 XML 的结构并且它有很多独特的标签,我发现使用第一种情况很容易。案例 2 和 3 只是向您展示如何针对更复杂的标签完成此操作。这是非常快的;我在 PHP 中最快的 XML 解析器是什么?
上讨论了速度在进行这样的基于标签的解析时要记住的最重要的事情是使用
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();
}
接受的答案给了我一个良好的开端,但带来了比我想要的更多的课程和更多的处理;所以这是我的解释:
$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();
Simple example:
public function productsAction()
{
$saveFileName = 'ceneo.xml';
$filename = $this->path . $saveFileName;
if(file_exists($filename)) {
$reader = new XMLReader();
$reader->open($filename);
$countElements = 0;
while($reader->read()) {
if($reader->nodeType == XMLReader::ELEMENT) {
$nodeName = $reader->name;
}
if($reader->nodeType == XMLReader::TEXT && !empty($nodeName)) {
switch ($nodeName) {
case 'id':
var_dump($reader->value);
break;
}
}
if($reader->nodeType == XMLReader::END_ELEMENT && $reader->name == 'offer') {
$countElements++;
}
}
$reader->close();
exit(print('<pre>') . var_dump($countElements));
}
}
XMLReader 在 PHP 站点 上有详细记录。这是一个 XML Pull 解析器,这意味着它用于迭代给定 XML 文档的节点(或 DOM 节点)。例如,您可以像这样浏览您提供的整个文档:
<?php
$reader = new XMLReader();
if (!$reader->open("data.xml"))
{
die("Failed to open 'data.xml'");
}
while($reader->read())
{
$node = $reader->expand();
// process $node...
}
$reader->close();
?>
然后由您决定如何处理 XMLReader::expand()返回的节点。
试试这个 https://pear.php.net/package/XML_Serializer/docs/latest/XML_Serializer/XML_Unserializer.html
缺点:它占用大量内存。 优点:它读取 XML 并将其转换为多维数组。再简单不过了:)
这对我来说效果更好更快
<html>
<head>
<script>
function showRSS(str) {
if (str.length==0) {
document.getElementById("rssOutput").innerHTML="";
return;
}
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
} else { // code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (this.readyState==4 && this.status==200) {
document.getElementById("rssOutput").innerHTML=this.responseText;
}
}
xmlhttp.open("GET","getrss.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>
<form>
<select onchange="showRSS(this.value)">
<option value="">Select an RSS-feed:</option>
<option value="Google">Google News</option>
<option value="ZDN">ZDNet News</option>
<option value="job">Job</option>
</select>
</form>
<br>
<div id="rssOutput">RSS-feed will be listed here...</div>
</body>
</html>
**后端文件**
<?php
//get the q parameter from URL
$q=$_GET["q"];
//find out which feed was selected
if($q=="Google") {
$xml=("http://news.google.com/news?ned=us&topic=h&output=rss");
} elseif($q=="ZDN") {
$xml=("https://www.zdnet.com/news/rss.xml");
}elseif($q == "job"){
$xml=("https://ngcareers.com/feed");
}
$xmlDoc = new DOMDocument();
$xmlDoc->load($xml);
//get elements from "<channel>"
$channel=$xmlDoc->getElementsByTagName('channel')->item(0);
$channel_title = $channel->getElementsByTagName('title')
->item(0)->childNodes->item(0)->nodeValue;
$channel_link = $channel->getElementsByTagName('link')
->item(0)->childNodes->item(0)->nodeValue;
$channel_desc = $channel->getElementsByTagName('description')
->item(0)->childNodes->item(0)->nodeValue;
//output elements from "<channel>"
echo("<p><a href='" . $channel_link
. "'>" . $channel_title . "</a>");
echo("<br>");
echo($channel_desc . "</p>");
//get and output "<item>" elements
$x=$xmlDoc->getElementsByTagName('item');
$count = $x->length;
// print_r( $x->item(0)->getElementsByTagName('title')->item(0)->nodeValue);
// print_r( $x->item(0)->getElementsByTagName('link')->item(0)->nodeValue);
// print_r( $x->item(0)->getElementsByTagName('description')->item(0)->nodeValue);
// return;
for ($i=0; $i <= $count; $i++) {
//Title
$item_title = $x->item(0)->getElementsByTagName('title')->item(0)->nodeValue;
//Link
$item_link = $x->item(0)->getElementsByTagName('link')->item(0)->nodeValue;
//Description
$item_desc = $x->item(0)->getElementsByTagName('description')->item(0)->nodeValue;
//Category
$item_cat = $x->item(0)->getElementsByTagName('category')->item(0)->nodeValue;
echo ("<p>Title: <a href='" . $item_link
. "'>" . $item_title . "</a>");
echo ("<br>");
echo ("Desc: ".$item_desc);
echo ("<br>");
echo ("Category: ".$item_cat . "</p>");
}
?>