我有一个大的XML文件,我想解析它并将其放入数据库中。例如,这个文件:
<aa>
<bb>Some text goes here and <br /> some more on a new line
there are other <junk/> tags that I want to keep ignoring
</bb>
</aa>
下面的代码使用SimpleXML来解析bb
标记中的文本内容,但它忽略了<br />
标记。如何修改代码以接受<br/>
而不接受<junk/>
?
$xml = simplexml_load_file("ab.xml");
foreach( $xml->bb as $bb ) {
// $bb now contains the text content of the element, but no tags
}
因为您可以准确地说出要删除哪些元素,所以使用xpath通常最容易做到这一点,方法是查询这些元素,然后删除它们。
在SimpleXML:中
$remove = '//junk'; // all <junk> tags anywhere
// simplexml
$sx = simplexml_load_string($xml);
foreach ($sx->xpath($remove) as $element) {
unset($element->{0});
}
在DOMDocument中:
$remove = '//junk'; // all <junk> tags anywhere
// dom
$doc = new DOMDocument();
$doc->loadXML($xml);
$xpath = new DOMXPath($doc);
foreach ($xpath->query($remove) as $element) {
$element->parentNode->removeChild($element);
}
完整示例(演示):
<?php
/**
* @link http://stackoverflow.com/a/26318711/367456
* @link https://eval.in/204702
*/
$xml = <<<BUFFER
<aa>
<bb>Some text goes here and <br /> some more on a new line
there are other <junk/> tags that I want to keep ignoring
</bb>
</aa>
BUFFER;
$remove = '//junk'; // all <junk> tags anywhere
// simplexml
$sx = simplexml_load_string($xml);
foreach ($sx->xpath($remove) as $element) {
unset($element->{0});
}
$sx->asXML('php://output');
// dom
$doc = new DOMDocument();
$doc->loadXML($xml);
$xpath = new DOMXPath($doc);
foreach ($xpath->query($remove) as $element) {
$element->parentNode->removeChild($element);
}
$doc->save('php://output');
输出:
<?xml version="1.0"?>
<aa>
<bb>Some text goes here and <br/> some more on a new line
there are other tags that I want to keep ignoring
</bb>
</aa>
<?xml version="1.0"?>
<aa>
<bb>Some text goes here and <br/> some more on a new line
there are other tags that I want to keep ignoring
</bb>
</aa>
如果您知道要保留哪些和要删除哪些,则可以剥离标签。
$xml = simplexml_load_file("ab.xml");
foreach( $xml->bb as $bb ) {
// This will strip everything but <br>
echo strip_tags($bb,"<br>");
}
我无法使用SimpleXML解决问题,但我成功地使用了递归方法的DOMElement。请注意,标记选择标准在递归函数中。
// SimpleXML can be used for the 'simple' cases
$xml = simplexml_load_file("file.xml");
$dom = dom_import_simplexml($xml);
// simpleXML and DOM works with the same underlying data structure, so you can use them interchangably
$aa_content = $xml->aa;
// using simpleXML, $aa is now: "Some text goes here and some more on a new line there are other tags that I want to keep ignoring"
// the <junk> tag is ignore, which is good; but the <br> tag is also ignored, which is bad
// the DOM method
foreach( $dom->childNodes as $node ) {
$textContent = parsePreserveTags($node);
}
function parsePreserveTags($domNode) {
// we want to preserve tags (for example, html formatting like <br>)
$result = '';//$domNode->nodeValue;
if( $domNode->hasChildNodes() ) {
foreach( $domNode->childNodes as $node ) {
// The constant XML_ELEMENT_NODE is defined here http://php.net/manual/en/dom.constants.php
// If node type is XML_ELEMENT_NODE it's a tag and it can have children.
// Otherwise, just get the (text) value.
if( $node->nodeType == XML_ELEMENT_NODE ) {
// Throw away nodes that match certain criteria
if( $node->nodeName == 'junk' )
continue;
if( $node->hasChildNodes() ) {
// example: "<p>...</p>"
$result .= '<' . $node->nodeName . '>' . parsePreserveTags($node)
. '</' . $node->nodeName . '>';
} else {
// example: "<br/>"
$result .= '<' . $node->nodeName . '/>';
}
} else {
// example: plain text node
$result .= $node->nodeValue;
}
}
}
return $result;
}