Miko Posted November 9, 2010 Share Posted November 9, 2010 Hello, I have made a PHP script that will feed an existing XML file. The file stores song details like, title, name of singer etc ... Now the file structure is like this for the moment: <?xml version="1.0" encoding="UTF-8"?> <music> <song> <title>Without me</title> <artist>Eminem</artist> <url>songs/coreIssues.mp3</url> <image>img/den.jpg</image> <discspeed>11</discspeed> </song></music> But I need it to be: <?xml version="1.0" encoding="UTF-8"?> <music> <song mainTitle="Eminem - Without me"> <title>Without me</title> <artist>Eminem</artist> <url>songs/coreIssues.mp3</url> <image>img/den.jpg</image> <discspeed>11</discspeed> </song></music> I need it to be like that so if I want to delete a song from the list I just check for the element which will have a unique mainTitle and then delete that element with all it's details. Now, I've searched on Google for some solutions but didn't find any yet. Anyone here knows how to do this? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/218242-xml-create-a-tag-element-with-attribute-and-give-it-a-value/ Share on other sites More sharing options...
requinix Posted November 9, 2010 Share Posted November 9, 2010 You don't need that mainTitle thing. Watch this: $xml = new DOMDocument(); $xml->loadXML( Without me Eminem songs/coreIssues.mp3 img/den.jpg 11 XML ); $title = "Without me"; $artist = "Eminem"; $xpath = new DOMXPath($xml); foreach ($xpath->query('//song[title="' . htmlentities($title) . '" and artist="' . htmlentities($artist) . '"]') as $node) { $node->parentNode->removeChild($node); } echo $xml->saveXML(); Though I suggest using ID numbers instead of relying on names. Quote Link to comment https://forums.phpfreaks.com/topic/218242-xml-create-a-tag-element-with-attribute-and-give-it-a-value/#findComment-1132466 Share on other sites More sharing options...
Miko Posted November 11, 2010 Author Share Posted November 11, 2010 Great! Thanks, it's working Quote Link to comment https://forums.phpfreaks.com/topic/218242-xml-create-a-tag-element-with-attribute-and-give-it-a-value/#findComment-1132945 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.