Php parses XML document attributes and edits the code
- // 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 xml file can be modified 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 ');
- }
- }
- }
- }
- ?>
Recommended for script School editing: php reading xml Class php xml Document parsing function learning instance php parsing XML data a piece of code PHP reading XML several methods php using DOM reading XML file code example learning php operating XML class DOMDocument |