PHP learning?? Xml

Source: Internet
Author: User
Tags dname xpath xsl xslt
"Create XML"

$dom =new DOMDocument ("1.0");
$book = $dom->appendchild ($dom->createelement (' book '));//Create root element
$title = $book->appendchild ($dom->createelement (' title '));//create a node under the root element
$title->appendchild ($dom->createtextnode (' PHP Cookbook '));//Create a child node under a node
$title->setattribute ("cover", "soft"); Create properties under//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");//Processing Properties
foreach ($ids as $id)
{
echo $id. "
";
}

$pname = $s->xpath ("/people/person/name");//Processing node

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");//processing nodes with child nodes

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. ":";

$FN 2= $xpath->query (' age ', $persion);
echo $fn 2->item (0)->firstchild->nodevalue. "
";

echo $persion->nodevalue. "
";
}

12-1.xml





Zhang June
20

Hebei
Shijiazhuang
Renmin East Road
13#



Liu June
21st

Jilin
Changchun
Jiefang South Road
25#



Jacky Chow
26

Guangdong
Shenzhen
Shennan Road
37#



Zhangqun
20

Guangxi
Nanning
Jiangnan Avenue
67#


7-2.xsl







"Apply XSLT"

You must open the PHP extension php_xsl

$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);

$results 2= $xslt->transformtouri ($xml, ' results.html ');

$results 3= $xslt->transformtodoc ($xml);

  • Related Article

    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.