Php code for modifying and adding xml node attributes

Source: Internet
Author: User
Php modifies the xml node attribute and adds the code for adding the xml node attribute. if you need it, you can refer to the php code for modifying and adding the xml node attribute for your reference.
Php modifies xml node attributes and adds code for xml node attributes. For more information, see.

1. xml file

The code is as follows:























2. php code

The code is as follows:


$ Dom = new DOMDocument ('1. 0 ');
$ Dom-> load ('x. XML ');
$ Em = $ dom-> getElementsByTagName ('Emotions ');
$ Em = $ em-> item (0 );
$ Items = $ em-> getElementsByTagName ('ITEM ');
Foreach ($ items as $ ){
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

The code is as follows:


// Read xml
$ Dom = new DOMDocument ('1. 0 ');
$ Dom-> load ('data. XML ');
$ Em = $ dom-> getElementsByTagName ('videoos '); // outermost node
$ Em = $ em-> item (0 );
$ Items = $ em-> getElementsByTagName ('video'); // Node
// Remove the following section if you do not need to read it.
Foreach ($ items as $ ){
Foreach ($ a-> attributes as $ B) {// $ B-> nodeValue; node attribute value $ B-> nodeName; node attribute name
Echo $ B-> nodeName;
Echo ":";
Echo $ B-> nodeValue;
Echo"
";
}
}
// Below is a new line written to xml
$ T = $ dom-> createElement ('video ');// $ T-> setAttribute ('title', '1 ');// $ T-> setAttribute ('src', '2 ');// $ T-> setAttribute ('IMG ', '1 ');// $ Em-> appendChild ($ t );//
$ Dom-> save ('data. XML ');
?>


The xml document at that time:

The code is as follows:










// The following file is modified later. you can modify the xml file.

The code is as follows:


$ 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.
$ AttribValue = $ attrib-> nodeValue; // nodeValue indicates the attribute content.

// Search for the node content whose property name is ip
If ($ attribName = 'IMG ')
{
// Find the node content whose property content is ip
If ($ attribValue = '1 ')
{
// Change the property to img and the content of img to 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.