如何在保持php中单词的原始顺序的同时解析XML?

时间:2017-06-15 13:22:55

标签: php xml xml-parsing

如何在保持原始单词顺序的同时解析类似于此处所示的xml?我的目的是仅提取第一个<span> - 标记和<strong> - 标记的内容,但单词的顺序应保持不变(输出应为:国际清算银行[BIZ] )。我尝试使用内置的php解析器(XML DOM和SimpleXML - Get),但是我无法保持单词的顺序。

<span class="full_collocation">
    the<strong class="tilde">Bank</strong> for International Settlements
</span> 
<span class="full_collocation">
    [<span class="or"><acronym title="or">or</acronym></span> BIZ]
</span>

1 个答案:

答案 0 :(得分:2)

使用DOMDocument,您应该能够轻松获得所需的值。看看这个例子:

$xmlString = '<root>
  <span class="full_collocation">
    the<strong class="tilde">Bank</strong> for International Settlements
  </span>
  <span class="full_collocation">
    [<span class="or"><acronym title="or">or</acronym></span> BIZ]
  </span>
</root>';

$dom = new DOMDocument();
$dom->loadXML($xmlString);
foreach($dom->documentElement->childNodes as $childNode) {
    echo trim($childNode->textContent); // prints "theBank for International Settlements" and "[or BIZ]"
}