Php learning ?? Xml [create xml]
$ Dom = new DOMDocument ("1.0 ");
$ Book = $ dom-> appendChild ($ dom-> createElement ('book'); // Create a root element
$ Title = $ book-> appendChild ($ dom-> createElement ('title'); // Create a node under the root element
$ Title-> appendChild ($ dom-> createTextNode ('php Cookbook'); // Create a subnode under the node
$ Title-> setAttribute ("cover", "soft"); // Create an attribute under the title node
$ Sklar = $ book-> appendChild ($ dom-> createElement ('author '));
// $ Sklar-> appendChild ($ dom-> createTextNode ('sklar '));
$ Name = $ sklar-> appendChild ($ dom-> createElement ('name '));
$ Name-> appendChild ($ dom-> createTextNode ('fsl '));
$ Trac = $ book-> appendChild ($ dom-> createElement ('author '));
$ Trac-> setAttribute ("cover", "feng ");
$ Trac-> setAttribute ("id", "123 ");
$ Trac-> appendChild ($ dom-> createTextNode ('trac '));
$ Dom-> formatOutput = true;
Echo $ dom-> saveXML ();
$ Dom-> save ('test. XML ');
$ Dom-> saveHTMLFile('1.html ');
1. htm
Php cookbook
Fsl
Trac
[Xml file processing method-xpath]
$ S = simplexml_load_file ("12-1.xml ");
$ Ids = $ s-> xpath ("/people/person/@ id"); // process attributes
Foreach ($ ids as $ id)
{
Echo $ id ."
";
}
$ Pname = $ s-> xpath ("/people/person/name"); // process nodes
Foreach ($ pname as $ name)
{
Echo $ name ."
";
}
$ Per = $ s-> xpath ("/people/person ");
Foreach ($ per as $ person ){
List ($ age) = $ person-> xpath ("age ");
List ($ city) = $ person-> xpath ("address/city ");
List ($ pro) = $ person-> xpath ("address/province"); // process nodes with subnodes
Echo $ city. "_". $ pro. ":". $ age ."
";
}
$ Dom = new DOMDocument;
$ Dom-> load ("12-1.xml ");
$ Xpath = new DOMXPath ($ dom );
$ Did = $ xpath-> query ("/people/person/@ id ");
Foreach ($ did as $ id)
{
Echo $ id-> nodeValue ."
";
}
$ Dname = $ xpath-> query ("/people/person/age ");
Foreach ($ dname as $ name)
{
Echo $ name-> nodeValue ."
";
}
$ Dper = $ xpath-> query ("/people/person ");
Foreach ($ dper as $ persion)
{
$ Fn = $ xpath-> query ('name', $ persion );
Echo $ fn-> item (0)-> firstChild-> nodeValue .":";
$ Fn2 = $ xpath-> query ('age', $ persion );
Echo $ fn2-> item (0)-> firstChild-> nodeValue ."
";
Echo $ persion-> nodeValue ."
";
}
12-1.xml
Zhang Jun
20
Hebei
Shijiazhuang
Renmin East Road
13 #
Liu Jun
21
Jilin province
Changchun
Jiefang South Road
25 #
Zhou Tai
26
Guangdong
Shenzhen
Shennan Road
37 #
Zhang Qun
20
Guangxi
Nanning
Jiangnan Avenue
67 #
7-2.xsl
[Apply xslt]
Php extension php_xsl must be enabled
$ Xsl = new DOMDocument;
$ Xsl-> load ('7-2. xsl ');
$ Xslt = new XSLTProcessor ();
$ Xslt-> importStylesheet ($ xsl );
$ Xml = new DOMDocument;
$ Xml-> load ('12-1. XML'); // double parameter problem
$ Results = $ xslt-> transformToXml ($ xml );
$ Results2 = $ xslt-> transformtouri('results.html ');
$ Results3 = $ xslt-> transformToDoc ($ xml );