I have the following xml structure :
<?xml version="1.0" encoding="utf-8" ?>
<nws>
<url>http://cpa.hypotheses.org/feed</url>
<url>http://news.ycombinator.com/rss</url>
<url>http://rss.slashdot.org/Slashdot/slashdot</url>
<url>http://feeds.feedburner.com/hackaday/LgoM</url>
</nws>
…And so on.
I wouldlike to remove the nth element. That’s it. All examples I’ve seen involve searching the node content using xmatch and I cannot search it because 1-it’s a complicated string and 2-I really don’t know the string I want to delete, but I do know the index.
I can read all the url nodes like this :
$.ajax({
type: 'GET',
url: 'conf.xml',
dataType: 'xml',
success: parseXml
});
$(xml).find("url").each(function() {
$("#output").append($(this).text());
// $("#output").append(": " + $(this).find("Title").text() + "<br />");
});
But I cannot seem to say “output only the second one”, let alone remove it.
One more thing. I understand that once I manipulated the XML DOM I have to write it back to the file using
echo $urlFile->asXML('myfile.xml');
But the process is somehow fuzzy (to What class does this “asXML” method belong to ? Is it even available in this context ? Do I have to load another library than just jquery.js ?)
I’m lost somewhere in the process ; Can somebody please post a rundown of the whole process ? ie
- read the file
- find, say the second node by the name of the node (not its content or attribute)
- delete it
- write back the modified representation of the DOM to the file
(ideally I’d like to be able to do some checks before writing back to the file, like empty lines, etc)
Even if the example is not functional, I just want to wrap my head around the concept.
OK, I solved half of the pb : I can now delete the nth node using its index, like this :
This outputs my list without the 3rd element. (but not as a DOM object). Still, Wohoo !
Now how can I get it back (as a DOM object) in a php var to write it back to the file ? Or using ajax maybe ? I can read a file from ajax, but not write to it, I don’t know how to do that, anyone ever done this that could point me in the right direction ? (save JS-processed XML to an actual file ?)
Regards, -ph