PHP parsing xml

Source: Internet
Author: User
Tags cdata tagname xml parser

1.xml instances

Test.xml

<?xml version= "1.0" encoding= "Utf-8"?>
<! DOCTYPE class SYSTEM "Test.dtd" >
<!--<! DOCTYPE class [
<! Element Class (student +) >
<! Element student (Name,age,sex) >
<! Element name (#PCDATA) >
<! Element Age (#PCDATA) >
<! Element sex (#PCDATA) >
]>-->
< class >
< students >
You can embed any content, including pictures, in CDATA.
By default, as long as it conforms to the writing syntax, the browser does not check whether it is logical, and we need to write our own code tests.
<!--<name> Goku &quot;&apos;&lt;&gt;</name>
<! [cdata[<age>500>< ' "" </age>]]>-->
<name> Wu Empty </name>
<age>500</age>
<sex> Men </sex>
People cannot have area attributes, not logical.
< area >500</area >
</Students >
< students >
<name> Bones Jing </name>
<age>1000</age>
<sex> Women </sex>
</Students >
</class >

Test.dtd

<! Element Class (student +) >
<! Element student (Name,age,sex) >
<! Element name (#PCDATA) >
<! Element Age (#PCDATA) >
<! Element sex (#PCDATA) >

Checkdtd.html

<title> Test dtd</title>
<meta http-equiv= "Content-type" content= "Text/html;charset=utf-8"/>
<script type= "Text/javascript" >
Create an XML parser
var xmldoc=new activexobject ("Microsoft.XMLDOM");
Turn on the check function
Xmldoc.validateonparse=true;
Specifies which XML file to validate
Xmldoc.load ("Test.xml");
If there is an error, the output
document.write ("error message" +xmldoc.parseerror.reason+ "<br/>");
document.write ("error message =" +xmldoc.parseerror.line+ "<br/>");
</script>

This method of detecting DTDs is only applicable to IE browser, which is a unique property of IE browser, and is not supported by other browsers.

2.php Technical Operation XML file

*php Dom

*php binding XPath to manipulate XML

*simplexml

Phpdom Parsing xml

Test.xml

<?xml version= "1.0" encoding= "Utf-8"?>
<! DOCTYPE class SYSTEM "Test.dtd" >
<!--<! DOCTYPE class [
<! Element Class (student +) >
<! Element student (Name,age,sex) >
<! Element name (#PCDATA) >
<! Element Age (#PCDATA) >
<! Element sex (#PCDATA) >
]>-->
< class >
< students >
<name>0</name>
<age>25</age>
<sex> Men </sex>
</Students >
< students >
<name>0</name>
<age>25</age>
<sex> Men </sex>
</Students >
< students >
<name>0</name>
<age>25</age>
<sex> Men </sex>
</Students >
</class >

phpdom.php

<meta http-equiv= "Content-type" content= "text/html charset=utf-8"/>
<?php
Gets the DOMDocument object.
$xmldoc =new DOMDocument ();
Specify the XML file to process, load the XML file
$xmldoc->load ("Test.xml");
Want to get every student's name
$students = $xmldoc->getelementsbytagname ("student");
Echo $students->length;
for ($i =0; $i < $students->length; $i +) {
$node = $students->item ($i);
Echo Getnodevalue ($node, "name"). " <br/> ";
Echo Getnodevalue ($node, "age"). " <br/> ";
Echo Getnodevalue ($node, "sex"). " <br/> ";
}
$node = $students->item (0);
/* $list = $node->getelementsbytagname ("name");
$list 1= $list->item (0)->nodevalue;
Echo $list 1;*/
Echo Getnodevalue ($node, "age");
Function Getnodevalue (& $node, $tagName) {
Return $node->getelementsbytagname ($tagName)->item (0)->nodevalue;
}

?>

Increase the operation Code of the node

addnode.php

<meta http-equiv= "Content-type" content= "text/html charset=utf-8"/>
<?php
//Gets DOMDocument object.
$xmldoc =new DOMDocument ();
//Specify the XML file to process, load the XML file
$xmldoc->load ("Test.xml");
Want to get each student's first name
$root = $xmldoc->getelementsbytagname ("Class")->item (0);
Create a student node
$name = $xmldoc->createelement ("student");
Create a name node
$name 1= $xmldoc->createelement ("name");
Assign a value to the name node
$name 1->nodevalue= "Xin Xin";
Attach the name node to the student node
$name->appendchild ($name 1);
Create age node
$name 2= $xmldoc->createelement ("ages");
Assign a value to the age node
$name 2->nodevalue= ";
Mount the age node under the student node
$name->appendchild ($name 2);
Create a gender node
$name 3= $xmldoc->createelement ("sex");
Assigning a
$name 3->nodevalue= "Male" to a gender node;
Attach the gender node to the student node
$name->appendchild ($name 3);
Finally, the student node is attached to the root node under the class
$root->appendchild ($name);
Write back to the XML file
$xmldoc->save ("Test.xml");
?

Operation to delete a node

<meta http-equiv= "Content-type" content= "text/html charset=utf-8"/>
<?php
Gets the DOMDocument object.
$xmldoc =new DOMDocument ();
$xmldoc->load ("Test.xml");
Get root node
$root = $xmldoc->getelementsbytagname ("Class")->item (0);
To delete a 3rd student
Get student Node
$students = $xmldoc->getelementsbytagname ("student");
Get a third student
$stu 1= $students->item (0);
Delete a third student
$root->removechild ($stu 1);
$stu 1->parentnode->removechild ($stu 1);
$xmldoc->save ("Test.xml");
echo "Delete succeeded";
?>

Actions to update node content

<meta http-equiv= "Content-type" content= "text/html charset=utf-8"/>
<?php
Gets the DOMDocument object.
$xmldoc =new DOMDocument ();
$xmldoc->load ("Test.xml");
Get student Node
$student = $xmldoc->getelementsbytagname ("Student")->item (0);
Get a third student
$stu _name= $student->getelementsbytagname ("name")->item (0);
Update the value of text
$stu _name->nodevalue+= "Eight commandments";
$xmldoc->save ("Test.xml");
echo "Update succeeded";
?>

Parsing XML with Java In contrast, using PHP to parse XML is simpler and easier to understand.

Share this today for the time being. Continue tomorrow.

The revolution has not yet succeeded, comrades still need to work hard!

PHP parsing 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.