I need to remove the parent node <feed> from this XML file so that I can use xpath without conflicts.
<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://ift.tt/r5ut6F">
<entry>
<contentVersion>preview-1.0.0</contentVersion>
<embeddability value="http://ift.tt/145SZ6Q" />
<openAccess value="http://ift.tt/1Aq01QG" />
<viewability value="http://ift.tt/145SYQo" />
<creator>Scott Blackwood</creator>
<date>2015-01-20</date>
<description>They bind up the three teenage girls who are working the counter, set fire to the shop, and disappear.</description>
<format>224 pages</format>
<format>book</format>
<identifier>Hbd5oAEACAAJ</identifier>
<identifier>ISBN:031637380X</identifier>
<identifier>ISBN:9780316373807</identifier>
<language>en</language>
<publisher>Little, Brown</publisher>
<subject>Fiction</subject>
<title>See How Small</title>
<title>A Novel</title>
</entry>
</feed>
I currently have this XML stored in the variable $xml via the following code:
$xml = file_get_contents(http://ift.tt/1Bv0SNQ);
Here is what I have tried, and it mostly works
$doc = new DOMDocument();
$doc->loadHTML($xml);
// Remove doctype node
$doc->doctype->parentNode->removeChild($doc->doctype);
// Remove feed element, preserving child nodes
$html = $doc->getElementsByTagName("feed")->item(0);
$fragment = $doc->createDocumentFragment();
while ($html->childNodes->length > 0) {
$fragment->appendChild($html->childNodes->item(0));
}
$html->parentNode->replaceChild($fragment, $html);
// Output results
echo $doc->saveXML();
But, my resulting text messes up the XML shebang line at the top. (adds a line):
<?xml version="1.0" standalone="yes"?>
<?xml version='1.0' encoding='UTF-8'??>
Anyone know how to prevent this?
No comments:
Post a Comment