Log in

View Full Version : simpleXML addChild with HTM tags



LOLFlash
04-08-2010, 07:08 PM
Hi all
How to addChild to simpleXML node if it has html entities?

I did try
myXML->addChild('form','<textfield>Hello World</textfield>');

it doesn't work

djr33
04-08-2010, 07:23 PM
I'm not sure, but I'm guessing that this might be confusing because XML and HTML use the same syntax (generally) so the processor gets confused when you are adding xml text to an xml object and everything will get messy.

Suggestions:
1. Use a more complex xml processor (if possible, I don't know)
2. Use a database (ALWAYS a good idea especially when it starts getting complex like this)
3. Escape the html characters, such as > to &gt;. You can use functions like htmlentities() for this.

LOLFlash
04-08-2010, 07:56 PM
Thanks for replay

what I want is to change my simpleXML node data

I have data in XMLfile
sourseXML=simplexml_load_file($link);

<htm>
<div><p>Hi Everyone</p></div>
<div><p>Hi Second time</p></div>
</htm>

and I want to change first node so my output comes in:
echo sourseXML->asXML()

<htm>
<div><form><textarea><p>Hi Everyone</p><textarea></form></div>
<div><p>Hi Second time</p></div>
</htm>


what technique I can use?

jscheuer1
04-09-2010, 12:17 AM
Moderator's note - LOLFlash's most recent post in this thread was automatically flagged by the board for some reason as possibly X rated or spam. This happens sometimes (rarely). I can't see why in this case though. Anyways, I have approved the post and deleted the others that were simply understandable duplicate attempts to post the information/question.

If this happens again LOLFlash, just be patient. I or one of the other mods will approve/validate the post soon as long as there is nothing bad in it, which obviously there isn't.

If anyone has any idea why the last post in this thread by LOLFlash was flagged by the board I'd love to know. Perhaps we can remove an overly aggressive keyword or change something else that caused this obviously genuine post to get flagged by the board.

LOLFlash, please accept our apologies and bear with us on this.

LOLFlash
04-09-2010, 07:56 PM
Thanks jscheuer1

just put a note on top of publish button with attention to wait and any flagged feedback will be helpful

I didn't know I sent and tried different buttons to rich djr33 while he was online

so "Post Replay" button didn't work and I used "Post Quick Replay" after all

LOLFlash
04-09-2010, 08:10 PM
Regarding PHP question

is anyone knows how to attaches CDATA to simpleXML?

I have filtered my xml with xpath

now I want replace node 2 text with text consisting html tags so my output will be

so my output:

<htm>
<div><p><![CDATA[<form>
<textarea>
<p>Hi Everyone</p>
<textarea></form>]]></p></div>
<div><p>Hi Second time</p></div>
</htm>


what is OK for me

but how to switch node 2 from text to CDATA?

I don't want to rebuild whole XML for purpose change only one node properties

I can access text through node[1][0] after xpath but I cant make it carrying html tags

any help appreciate

traq
04-10-2010, 12:58 AM
the most semantic way would be to add the <p> content as another xml tag:


<?xml version="1.0"?>
<htm>
<div>
<p>
Hi Everyone
</p>
</div>
</htm>
you could then access the content normally and add the html tags via your php script:

<?php

$sourseXML=simplexml_load_file($link);

echo '<p>'.$sourseXML->htm->div->p.'</p>';

?>
the alternative would be to rewrite your xml - perhaps using BBCode-style tags - and then use preg_replace() to convert it all. As djr mentions, it would be simpler to use a database. If you're not searching/manipulating the contents of this file, you could also simply save it as a text file and just include() it directly.