PHPはXMLを作成して解析します



Php Creates Parses Xml



デリミタ: <<
複雑で多様な文字列を格納するために使用されます。

<<<EOF <a href='javascript:edit('asd', 'aaa')'>edita> | <font color='#ccc'>deletefont> <a href='javascript:confirmurl('?m=admin&posid=12')'>deletea> | <font color='red'>Enablefont>a> | <a href='javascript:preview('3','ds')'><font color='green'>Demofont>a> EOF

次の点に注意する必要があります:<< with EOFその前にスペースを入れることはできません。もちろん、他のスタイルに置き換えることもできます。決まったルールはありません。例:<<<_xml with _xmlそれも可能です。



xmlページの小さなケース:

<root> <version>1.0version> <info>xml parsing testinfo> <user> <name>Mengma Technologyname> <url>http://www.itmoom.comurl> <author sex='male'>Huang Nimaauthor> user> <user> <name>Mengmaname> <url>http://www.itmoom.comurl> <author sex='Female'>Li Siauthor> user> <user> <name>EDonkeyname> <url>http://www.test.comurl> <aurhor sex='male'>Zhang Sanaurhor> user> root>

xmlファイルを生成します

上記のxmlファイルに基づいて、ここでsimpleXML()関数とasXML()関数を使用します。



<root> <version>1.0version> <info>xml parsing testinfo> <user> <name>Mengma Technologyname> <url>http://www.itmoom.comurl> <author sex='male'>Huang Nimaauthor> user> <user> <name>Mengmaname> <url>http://www.itmoom.comurl> <author sex='Female'>Li Siauthor> user> <user> <name>EDonkeyname> <url>http://www.test.comurl> <aurhor sex='male'>Zhang Sanaurhor> user> root> _xml //Create a simplexml object and pass in the xml string $_sxe = new SimpleXMLElement($_xml) //Generate xml file $_sxe->asXML('ttt.xml') ?>

これにより、対応するページの場所に「ttt.xml」という名前のファイルが生成されます。

XMLファイルをロードする

使用simplexml_load_file()関数はxmlをロードできます。
xmlをロードして入力します。 出力は引き続きasXML()関数を使用します

//Load xml file, simplexml $_sxe = simplexml_load_file('ttt.xml') //Test output, you cannot use echo to output directly here, of course you can also use print_r() to output echo $_sxe->asXML() ?>

XMLファイルを解析します

attributes()属性を印刷できます。



xpathを使用してxmlノードを取得します

//Load xml file, simplexml $_sxe = simplexml_load_file('ttt.xml') //Get the value of version, first get the path to print the output value, root is the root path //Then assign the found value to the variable. Because it is in the form of an array, all can be output segmented or traversed. // $_version = $_sxe->xpath('/root/version') // echo $_version[1] //Because it is an array, it can also be output in the form of traversal. foreach ($_sxe->xpath('/root/version') as $key) { echo '['.$key.']' } ?>

DOMDocumentはXMLを操作します

xmlをロードします

//Create dom object $_doc = new DOMDocument() //Load xml file $_doc->load('ttt.xml') //Take the version tag value, getElementsByTagName finds the incoming tag $_version = $_doc->getElementsByTagName('version') //Print out the value of the first group. item(0) represents the number, nodeValue represents the value in the tag // echo $_version->item(0)->nodeValue //Traverse loop output // foreach ($_version as $_v) { // echo $_v->nodeValue // } ?>

xmlを作成する