php: replace text in xml doc
- Started
- Last post
- 27 Responses
- acescence0
i threw this thing together, you're welcome to use it and hack away at it if you'd like. however it uses a DATABASE, but it's all very simple..
all the files are here:
http://culturekiller.com/xml/xml…you need to edit include/configuration.php with your db details
class.page.php in include/objects has the SQL query at the top that will create the table.i never created the delete page, you can figure that out.
- felizfeliz0
well, ok. but i'm jsut a beginner with php so all that sounds a bit complex to me. I've not been able to find a good tutorial that I can learn from.
- acescence0
well that will set every leaf node to goodbye, but it's close. i still think you're doing this all ass backwards. why not read in the whole doc to a native php datatype, do all your manipulation, then write it all back out to an xml file? you could even just store it in a file as a serialized php object and not have to fuck with dom manipulation, then just overwrite a new xml file when it's updated.
- felizfeliz0
ok.. finally got the answer thanks to some help:
http://www.phpbuilder.com/board/…
<?php
$dom = new DOMDocument();
$dom->load('test.xml');$nodes = $dom->getElementsByTagName('leaf...
foreach($nodes as $n) {
$n->firstChild->nodeValue = 'Goodbye';
}
$dom->save("test.xml");
echo $dom->saveXML();
?>
- rounce0
@felizfeliz: I get that you're frustrated with the speed at how complex these things become, but I find it's like composing a piece of music. You could have all the parts played by the same instrument, but the amount of playing skill and acoustic versatility required, would far outweigh putting in the compartively small amount of effort to find a few more musicians, who are able (and at a standard you require) to play the part you've selected for them.
- vaxorcist0
CDATA is an odd fish.... You have to use it because clients fill in crap from MS-Word and such, always containing illegal characters that fake out the XML parser, but CDATA is a total violation of the whole idea of XML, where structured information should have some order....
of course, the real problem with XML CMS's is that XML demands order and clients demand to be able to re-arrange randomly in my experience...
- 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....
- 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
- 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:
- 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
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 :
- 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.
- 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
- felizfeliz0
don't see the answer there neverblink.
- 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
ah... ok... well i haven't a clue how to do this simple task.
- 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.