Php learning ?? Xml

Source: Internet
Author: User
Tags xslt
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 );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.