简体   繁体   English

错误的PHP domDocument XML解析

[英]error php domDocument xml parsing

Does anybody know how I can get this xml into the domDocument? 有人知道如何将这个xml放入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. 我看到的是feed是顶级元素,所以对我来说是个谜。 This only happens if I run it threw the domDocument and not when I echo it out directly. 这仅在我运行它抛出domDocument的情况下发生,而当我直接将其回显时则不会发生。 The xml validator also gives no errors. xml验证程序也没有错误。

This is the xml: 这是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;'><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 感谢副词,理查德

从字符串加载的最佳方法是使用loadXML函数。

$dom->loadXML($xml); doesn't return an object when called in an object context (ie $obj-> loadxml()) but modifies the internal state of the object itself. 在对象上下文中调用时不会返回对象(即$ obj-> loadxml()),但会修改对象本身的内部状态。

eg 例如

$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 ? 为什么不直接使用file_put_contents? like this 像这样

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM