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 ');
}
}
}
}
?>