我有一个大型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
}
答案 0 :(得分:1)
如果您知道要保留哪个标签以及要删除哪个标签,则可以删除标签。
$xml = simplexml_load_file("ab.xml");
foreach( $xml->bb as $bb ) {
// This will strip everything but <br>
echo strip_tags($bb,"<br>");
}
答案 1 :(得分:1)
我无法使用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;
}
答案 2 :(得分:1)
正如您可以确切地说要删除哪些元素一样,通常通过查询这些元素然后删除它们来使用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);
}
完整示例(Demo):
<?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>