Reputation: 27
How can I parse xml similar to the one shown here, while keeping the order of the original words? My aim is to extract only the content of the first <span>
-tag and the <strong>
-tag, but the order of words should stay the same (output should be: The Bank for International Settlements [BIZ]). I tried it using the built-in php parsers (XML DOM and SimpleXML - Get), however I wasn't able to keep the order of words.
<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>
Upvotes: 0
Views: 107
Reputation: 1947
With DOMDocument, you should be able to easily get the value you want. Check out this example:
$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]"
}
Upvotes: 2