开发者

error php domDocument xml parsing

开发者 https://www.devze.com 2022-12-17 03:39 出处:网络
Does anybody know how I can get this xml into the domDocument? Right now, it throws an error saying it is missing a top level element.

Does anybody know how I can get this xml into the domDocument?

Right now, it throws an error saying it is missing a top level element. What I see is that feed is the toplevel element, so it's a mystery to me. This only happens if I run it threw the domDocument and not when I echo it out directly. The xml validator also gives no errors.

This is the xml:

$xml="<?xml version='1.0' encoding='UTF-8'?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:gphoto='http://schemas.google.com/photos/2007' xmlns:openSearch='http://a9.com/-/spec/opensearch/1.1/' xmlns:gd='http://schemas.google.com/g/2005' gd:etag='W/&quot;CEANSXg7cSp7ImA9WxBRGUU.&quot;'><id>http://picasaweb.google.com/data/feed/user/rich3607/albumid/5424469095880909921</id><updated>2010-01-08T20:33:18.609Z</updated><category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/photos/2007#album'/><title>tsa_ch</title><subtitle>chauffeurs</subtitle><rights>private</rights><icon>http://lh6.ggpht.com/_Q3ykLtTspk4/S0eV5xMQ5GE/AAAAAAAAAIY/req43lh-f4I/s160-c/Tsa_ch.jpg</icon><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://picasaweb.google.com/data/feed/api/user/rich3607/albumid/5424469095880909921?authkey=Gv1sRgCMvZ79uI6Z__ZQ'/><link rel='alternate' type='text/html' href='http://picasaweb.google.com/rich3607/Tsa_ch?authkey=Gv1sRgCMvZ79uI6Z__ZQ'/><link rel='http://schemas.google.com/photos/2007#slideshow' type='application/x-shockwave-flash' href='http://picasaweb.google.com/s/c/bin/slideshow.swf?host=picasaweb.google.com&amp;RGB=0x000000&amp;feed=http%3A%2F%2Fpicasaweb.google.com%2Fdata%2Ffeed%2Fapi%2Fuser%2Frich3607%2Falbumid%2F5424469095880909921%3Fauthkey%3DGv1sRgCMvZ79uI6Z__ZQ%26alt%3Drss'/><link rel='http://schemas.google.com/photos/2007#report' type='text/html' href='http://picasaweb.google.com/lh/reportAbuse?uname=rich3607&amp;aid=5424469095880909921'/><link rel='http://schemas.google.com/acl/2007#accessControlList' type='application/atom+xml' href='http://picasaweb.google.com/data/feed/api/user/rich3607/albumid/5424469095880909921/acl?authkey=Gv1sRgCMvZ79uI6Z__ZQ'/><link rel='self' type='application/atom+xml' href='http://picasaweb.google.com/data/feed/api/user/rich3607/albumid/5424469095880909921?start-index=1&amp;max-results=500&amp;kind=tag'/><author><name>rich</name><uri>http://picasaweb.google.com/rich3607</uri></author><generator version='1.00' uri='http://picasaweb.google.com/'>Picasaweb</generator><openSearch:totalResults>4</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>500</openSearch:itemsPerPage><gphoto:id>5424469095880909921</gphoto:id><gphoto:location/><gphoto:access>private</gphoto:access><gphoto:timestamp>1262937600000</gphoto:timestamp><gphoto:numphotos>2</gphoto:numphotos><gphoto:numphotosremaining>998</gphoto:numphotosremaining><gphoto:bytesUsed>5866</gphoto:bytesUsed><gphoto:user>rich3607</gphoto:user><gphoto:nickname>rich</gphoto:nickname><gphoto:allowPrints>true</gphoto:allowPrints><gphoto:allowDownloads>true</gphoto:allowDownloads><entry gd:etag='W/&quot;CEANSXg7cSp7ImA9WxBRGUU.&quot;'><id>http://picasaweb.google.com/data/entry/user/rich3607/albumid/5424469095880909921/tag/smarty</id><updated>2010-01-08T20:33:18.609Z</updated><category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/photos/2007#tag'/><title>smarty</title><summary>smarty</summary><link rel='alternate' type='text/html' href='http://picasaweb.google.com/lh/searchbrowse?q=smarty&amp;psc=G&amp;uname=rich3607&amp;filter=0'/><link rel='self' type='application/atom+xml' href='http://picasaweb.google.com/data/entry/api/user/rich3607/albumid/5424469095880909921/tag/smarty?authkey=Gv1sRgCMvZ79uI6Z__ZQ'/><author><name>rich</name><uri>http://picasaweb.google.com/rich3607</uri></author></entry><entry gd:etag='W/&quot;CEANSXg7cSp7ImA9WxBRGUU.&quot;'&开发者_开发技巧gt;<id>http://picasaweb.google.com/data/entry/user/rich3607/albumid/5424469095880909921/tag/00tx343</id><updated>2010-01-08T20:33:18.609Z</updated><category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/photos/2007#tag'/><title>00tx343</title><summary>00tx343</summary><link rel='alternate' type='text/html' href='http://picasaweb.google.com/lh/searchbrowse?q=00tx343&amp;psc=G&amp;uname=rich3607&amp;filter=0'/><link rel='self' type='application/atom+xml' href='http://picasaweb.google.com/data/entry/api/user/rich3607/albumid/5424469095880909921/tag/00tx343?authkey=Gv1sRgCMvZ79uI6Z__ZQ'/><author><name>rich</name><uri>http://picasaweb.google.com/rich3607</uri></author></entry><entry gd:etag='W/&quot;CEANSXg7cSp7ImA9WxBRGUU.&quot;'><id>http://picasaweb.google.com/data/entry/user/rich3607/albumid/5424469095880909921/tag/xpl-amsterdam</id><updated>2010-01-08T20:33:18.609Z</updated><category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/photos/2007#tag'/><title>xpl-amsterdam</title><summary>xpl-amsterdam</summary><link rel='alternate' type='text/html' href='http://picasaweb.google.com/lh/searchbrowse?q=xpl-amsterdam&amp;psc=G&amp;uname=rich3607&amp;filter=0'/><link rel='self' type='application/atom+xml' href='http://picasaweb.google.com/data/entry/api/user/rich3607/albumid/5424469095880909921/tag/xpl-amsterdam?authkey=Gv1sRgCMvZ79uI6Z__ZQ'/><author><name>rich</name><uri>http://picasaweb.google.com/rich3607</uri></author><gphoto:weight>2</gphoto:weight></entry><entry gd:etag='W/&quot;CEANSXg7cSp7ImA9WxBRGUU.&quot;'><id>http://picasaweb.google.com/data/entry/user/rich3607/albumid/5424469095880909921/tag/stupid</id><updated>2010-01-08T20:33:18.609Z</updated><category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/photos/2007#tag'/><title>stupid</title><summary>stupid</summary><link rel='alternate' type='text/html' href='http://picasaweb.google.com/lh/searchbrowse?q=stupid&amp;psc=G&amp;uname=rich3607&amp;filter=0'/><link rel='self' type='application/atom+xml' href='http://picasaweb.google.com/data/entry/api/user/rich3607/albumid/5424469095880909921/tag/stupid?authkey=Gv1sRgCMvZ79uI6Z__ZQ'/><author><name>rich</name><uri>http://picasaweb.google.com/rich3607</uri></author></entry></feed>";

this does not work

@$dom = new DOMDocument();      
$dom_xml = $dom->loadXML($xml); 
$output= $dom_xml->saveXML(); 
echo $output;

thanks in adv, Richard


The best way to load from a string is to use the loadXML function.


$dom->loadXML($xml); doesn't return an object when called in an object context (i.e. $obj->loadxml()) but modifies the internal state of the object itself.

e.g.

$doc = new DOMDocument();      
$doc->loadXML($xml) or die('loadxml failed');
$output= $doc->saveXML(); 
echo phpversion(), "\n";
echo '|savexml|=', strlen($output), "\n";
echo '|children| = ', $doc->documentElement->childNodes->length, "\n";

prints (on my machine)

5.3.1
|savexml|=5840
|children| = 33


Why not just use file_put_contents ? like this

file_put_contents("myxml.xml",$xml);
0

精彩评论

暂无评论...
验证码 换一张
取 消