php: replace text in xml doc
- Started
- Last post
- 27 Responses
- felizfeliz
Please can someone show me how to replace the text in a XML document node.
For example we have this simple xml document:
<?xml version="1.0"?>
<root>
<branch>
<leaf><![CDATA[Hello]]></leaf>
</branch>
</root>I want to change the word 'Hello' to 'Goodbye'.
How do I do that in php?
- airey0
<?xml version="1.0"?>
<root>
<branch>
<leaf><![CDATA[Goodbye]]></leaf>
</branch>
</root>solved.
- felizfeliz0
thanks yeah, and with PHP can you show me the php code to do that?
- felizfeliz0
thanks acescence, but it loses the CDATA tags.
<leaf><![CDATA[Hello]]></leaf>
becomes:
<leaf>Goodbye</leaf>
I need:
<leaf><![CDATA[Goodbye]]></leaf>
- acescence0
^ you would basically just build a new xml doc
- felizfeliz0
so there's no easy way to simply navigate to a particular node and replace the value? I have to rebuild the whole xml document?
- acescence0
i think you can create a new node and use replace child, i don't know, haven't used dom stuff much. i don't think you can set the content like a normal text node.
- felizfeliz0
ah... ok... well i haven't a clue how to do this simple task.
- felizfeliz0
can javascript do this?
why in web design are the simplest things undocumented and frustratingly difficult to find simple solutions for. why is it so bloody complex and annoying. just to change one word in a xml document, that's all i want to do...
- felizfeliz0
don't see the answer there neverblink.
- neverblink0
get that xml into a variable inside php ($xmlstr in this example)
$xml = new SimpleXMLElement($xmlstr);
$xml->root->branche[0]->leave[0] = "<![CDATA[Goodbye]]>";- doesn't work. the < > are changed to < and >felizfeliz
- acescence0
simplexml can't create proper cdata nodes. it's really more for just reading xml. that above code will result in conversion of the < > entities. you have to use the php xml dom functions.
- felizfeliz0
am I even going about this the right way - in as much as, is php the right way to do this?
The xml dom functions don't have a command to replace a CDATA formatted node - I'll have to remove the node, write the CDATA node and then append it back to the xml dom.
What a load of nonsense...
- THATS NOT TRUE , i just read and theres plenty of choice :
DomNode->replace_nod...
DomNode->set_content
etc..spraycan
- THATS NOT TRUE , i just read and theres plenty of choice :
- rounce0
"am I even going about this the right way - in as much as, is php the right way to do this?"
You should be thinking: are flat xml files the way to do this?
When you were planning your method of content delivery why didn't you think about editing said content and it's implications?
- felizfeliz0
Ok so how do i create a simple CMS for a flash site. My flash site will work perfectly bringing in all the content from an XML file. so I was trying to make a simple editor for the xml docs.
But if you know a better method please do tell. And don't start mentioning databases. Simple is what I'm after... Maybe I should just get this:
- felizfeliz0
I'm through - I'm throwing in the towel. I can't make a website, I'm too confused about which languages to even use there's so many now.
Why, as the internet has developed have we gone from just one language 'html' to php, css, msql, flash, javascript, jquery, ajax...
what the hell is going on!
I can't stand it.
My client wants a simple flash website and with cms. Why isn't that simple to do. Why isn't there a book on it. why isn't there a simple tutorial on it. Why can't flash do the whole thing anyway. Why do I need to use php. God it's pants. Flash is so limiting and sitting outside of the general way the internet works that I don't even want to use it anymore. I want to tell my clients it'll be better to do it in html. but will it' then I'll have to spend weeks hacking it to work in IE, and learn not just html, but css and javascript and some jquery and some msql and lets not forget php perhaps.
So many bloody different languages each doing their own thing and then you need to hack everything to make it bloody work across different browsers/computers.
Web developing in 2009 is ruddy mess and it's pissed me off!
And there's still no simple solution for my original question.
This sucks.
- flash jumped the shark a little while ago.... AS3 is a cool idea but uses all your brain-RAM at once..vaxorcist
- vaxorcist0
3 possible methods:
1. $string = str_replace("hello","whatever new stuff",$string);
this will globally change hello to whatever new stuff, in the whole file, all at once2. open and loop through:
$rows = file("file.xml");
for($i=0;$i<sizeof($rows);$i++){
$this_row = $rows[$i];
// do stuff, use if/else/etc
}
// write output string to file3. use a library, ideally a good idea but may take more time to learn the library than to hack out a few lines of code
4. use a text-editor if it's a one-time thing....