Php code for modifying and adding xml node attributes

Source: Internet
Author: User

Php modifies xml node attributes and adds the code for adding xml node attributes. For more information, see

Php code for modifying and adding xml node attributes for your reference.
Php modifies xml node attributes and Adds code for xml node attributes. For more information, see.

1. xml file

Copy the Code as follows:


<?xml version="1.0" encoding="UTF-8" ?><clientSet><server url="192.168.0.180" port="1935" /><rootPath value="" />

 

2. php code

<?$dom=new DOMDocument('1.0');$dom->load('x.xml');$em=$dom->getElementsByTagName('emotions');$em=$em->item(0);$items=$em->getElementsByTagName('item');foreach($items as $a){foreach($a->attributes as $b){if($b->nodeValue=='Birthday'){$a->setAttribute('name','nBirthday');}}}$t=$dom->createElement('item');$t->setAttribute('name','x');$t->setAttribute('src','www.baidu.com');$t->setAttribute('duration','duration');$em->appendChild($t);$dom->save('x.xml');?>

PHP parses XML document attributes and edits

<? Php // read xml $ dom = new DOMDocument ('1. 0'); $ dom-> load ('data. xml '); $ em = $ dom-> getElementsByTagName ('videos'); // The outermost node $ em = $ em-> item (0 ); $ items = $ em-> getElementsByTagName ('video'); // node // if you do not need to read it, remove the following section to foreach ($ items as $) {foreach ($ a-> attributes as $ B) {// $ B-> nodeValue; node attribute value $ B-> nodeName; node attribute name echo $ B-> nodeName; echo ":"; // www.jbxue.com echo $ B-> nodeValue; echo "<br/> ";}} // below is a new line written to xml $ T = $ dom-> createElement ('video'); // <video $ t-> setAttribute ('title', '1 '); // <video name = "data" $ t-> setAttribute ('src', '2 '); // <video name = "data" src = "2" $ t-> setAttribute ('img ', '1 '); // <video name = "data" img = "1" $ em-> appendChild ($ t ); // <video name = "data" img = "1"/> $ dom-> save ('data. xml');?>
 
The xml document at that time:
<?xml version="1.0"?> <videos>  <video img="a" url="1" title="1" nickname="1" tag="1" vid="1" star="1"/>  <video img="b" url="2" title="2" nickname="2" tag="2" vid="2" star="2"/>  <video img="c" url="3" title="3" nickname="3" tag="3" vid="3" star="3"/>  <video title="d" src="2" img="1"/> </videos> 


// You can modify the xml after modification.
<? Php $ doc = new DOMDocument (); $ doc-> load ('data. xml'); // find the videos node $ root = $ doc-> getElementsByTagName ('videoos '); // The first videos node $ root = $ root-> item (0 ); // find the video node under the videos node $ userid = $ root-> getElementsByTagName ('video'); // traverse all video nodes foreach ($ userid as $ rootdata) {// traverse all attributes of each video node foreach ($ rootdata-> attributes as $ attrib) {$ attribName = $ attrib-> nodeName; // nodeName is the attribute name $ attribV Alue = $ attrib-> nodeValue; // nodeValue indicates the attribute content // if ($ attribName = 'img ') {// if ($ attribValue = '1') {// modify the node content whose attribute is "img" and "img content is" 1 "to" image; $ rootdata-> setAttribute ('img ', 'image'); $ doc-> save ('data. 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.