Results 1 to 7 of 7

Thread: simpleXML addChild with HTM tags

  1. #1
    Join Date
    Apr 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default simpleXML addChild with HTM tags

    Hi all
    How to addChild to simpleXML node if it has html entities?

    I did try
    Code:
     myXML->addChild('form','<textfield>Hello World</textfield>');
    it doesn't work
    Last edited by LOLFlash; 04-08-2010 at 07:13 PM.

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Apr 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default merge XML

    Thanks for replay

    what I want is to change my simpleXML node data

    I have data in XMLfile
    sourseXML=simplexml_load_file($link);
    HTML Code:
    <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()
    HTML Code:
    <htm>
    <div><form><textarea><p>Hi Everyone</p><textarea></form></div>
    <div><p>Hi Second time</p></div>
    </htm>

    what technique I can use?

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    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.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #5
    Join Date
    Apr 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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

  6. #6
    Join Date
    Apr 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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:
    Code:
    <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
    Last edited by LOLFlash; 04-09-2010 at 09:21 PM.

  7. #7
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    the most semantic way would be to add the <p> content as another xml tag:

    Code:
    <?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 Code:
    <?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.
    Last edited by traq; 04-10-2010 at 01:05 AM.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •