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