DOMElement
DOMElement DOMDocument::createElement ( string $name [
創建節點元素
String $name
String $value
•DOMNode DOMNode::appendChild ( DOMNode $newnode )
添加子節點
DOMNode $newnode
在dom操作中
•string DOMDocument::saveXML
保存至某個字符串中
•int DOMDocument::save ( string $filename )
保存至某個文件中
String $filename
•DOMNode DOMNode::removeChild ( DOMNode $oldnode )
刪除節點
DOMNode $oldnode
•DOMNode DOMNode::replaceChild ( DOMNode $newnode
DOMNode $newnode
DOMNode $oldnode
•DOMAttr DOMElement::setAttribute( string $name
string $name
string $value
DOMAttr DOMElement::setAttribute( string $name
string $name
string $value
•bool DOMElement::removeAttribute ( string $name )
string $name
•string DOMElement::getAttribute ( string $name )
string $name
DOMDocument還是PHP
$xmlstring = <<<XML
<?xml version=
<document>
<cmd attr=
<login>imdonkey</login>
</document>
XML;
$dom = new DOMDocument();
$dom
print_r(getArray($dom
function getArray($node) {
$array = false;
if ($node
foreach ($node
$array[$attr
}
}
if ($node
if ($node
$array[$node
} else {
foreach ($node
if ($childNode
$array[$childNode
}
}
}
} else {
return $node
}
return $array;
}
SimpleXML
SimpleXML是PHP
$string = <<<XML
<?xml version=
<document>
<cmd>login</cmd>
<login>imdonkey</login>
</document>
XML;
$xml = simplexml_load_string($string);
print_r($xml);
$login = $xml
print_r($login);
$login = (string) $xml
print_r($login);
SimpleXML的優點是開發簡單
XMLReader
XMLReader也是PHP
以一個利用google天氣api獲取信息的例子展示下XMLReader的使用
$xml_uri =
$current = array();
$forecast = array();
$reader = new XMLReader();
$reader
while ($reader
//get current data
if ($reader
while($reader
$name = $reader
$value = $reader
$current[$name] = $value;
}
}
//get forecast data
if ($reader
$sub_forecast = array();
while($reader
$name = $reader
$value = $reader
$sub_forecast[$name] = $value;
}
$forecast[] = $sub_forecast;
}
}
$reader
XMLReader和XML Parser類似
由於XMLReader基於libxml
From:http://tw.wingwit.com/Article/program/PHP/201311/20888.html