<?php$dom = new DOMDocument();$xml = <<<XML<?xml version="1.0" encoding="utf-8"?><root><item id="i1">sdfds</item><item id="i2">sxfds</item></root>XML;$dom->preserveWhiteSpace = false;$dom->loadxml($xml);//$dom->load("getId.xml");$root = $dom->getElementsByTagName('root')->item(0);$childs = $root->childNodes;for($i=0; $i<$childs->length;$i++){$item = $childs->item($i);$item->setIdAttribute('id', true);}echo $dom->getElementById("i1")->tagName;echo $dom->getElementById("i2")->tagName;?>我用load()方法可以讀成功,但是用loadxml方法卻報錯,請問什么原因?
1 回答

侃侃爾雅
TA貢獻1801條經驗 獲得超16個贊
1.定義和用法
simplexml_load_file() 函數把 XML 文檔載入對象中。
如果失敗,則返回 false。
2.語法
simplexml_load_file(file,class,options,ns,is_prefix)參數 描述
file 必需。規定要使用的 XML 文檔。
class 可選。規定新對象的 class。
options 可選。規定附加的 Libxml 參數。
ns 可選。
is_prefix 可選。
3.實例
例子 1. Interpret an XML document
代碼如下
<?php
// The file test.xml contains an XML document with a root element
// and at least an element /[root]/title.
if (file_exists('test.xml')) {
$xml = simplexml_load_file('test.xml');
var_dump($xml);
} else {
exit('Failed to open test.xml.');
}
?>
This script will display, on success:
SimpleXMLElement Object
(
[title] => Example Title
...
)
- 1 回答
- 0 關注
- 189 瀏覽
添加回答
舉報
0/150
提交
取消