/    Sign up×
Community /Pin to ProfileBookmark

I am trying to put an html string inside of xml with php like this

I am trying to put an html string inside of xml with php like this:

<?php
$xml_resource = new SimpleXMLElement(‘stuff.xml’, 0, true);
$xml_resource->content = ‘<u>111111111111111111111111111111111 text</u>’;
$xml_resource->asXML(‘stuff.xml’);
?>


————————————————————-

results here:
———————————————————–

<?xml version=”1.0″?> <data>
<content id=”pic1″ frame=”1″ xpos=”22″ ypos=”22″ width=”11″ height=”11″>&lt;![&lt;u&gt;111111111111111111111111111111111 text&lt;/u&gt;&gt;</content> </data>

so my problem is i want the same <u></u> tags in xml but when i write then this change to any other formate so please help me urgently.thanks.

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@eval_BadCode_Mar 06.2011 — You're dealing with 2 markup languages. The way they represent information is nearly identical. So your issue is that you want to put HTML ([U]hyper-text[/U] markup) inside of XML ([U]extensible[/U] markup), but you're having issues with syntax. This page should help you:

http://www.w3schools.com/xml/xml_syntax.asp


Here are 3 solutions to your problem, you'll have to think about which one makes the most sense.

  • 1. It could be inherent that all <content> </content> is going to be wrapped in <u></u> tags, in which case a comment or just simply remembering that is all you need to do. Just remove the hyper-text markups in this case and store the plain string.


  • 2. You can store the htmlTag as an attribute, I don't recommend this solution.


  • [CODE]<?xml version="1.0"?>
    <data>
    <content id="pic1" frame="1" xpos="22" ypos="22" width="11" height="11" htm_tag="U">111111111111111111111111111111111 text
    </content>
    </data>
    [/CODE]


  • 3. You can correctly use XML and write all markup in XML. That way you get all of the benefits of XML. Here is an example:


  • [CODE]<?xml version="1.0"?>
    <data>
    <content>
    <picture id="pic1" frame="1">
    <title>111111111111111111111111111111111 text</title>
    <offsets>
    <x>22</x>
    <y>22</y>
    <z>1</z>
    </offsets>
    <dimensions>
    <width>11</width>
    <height>11</height>
    </dimensions>
    </picture>
    </content>
    </data>
    [/CODE]


    Now when you render the data, you can decide for yourself if you really want that text to be underlined.
    Copy linkTweet thisAlerts:
    @wbxpertsauthorMar 06.2011 — 

    i import dynamically records in the xml
    ----------------------------------------------------------------


    this is my php work
    ----------------------------------------------------------------



    include("db.php");

    $main='<settings>

    <mediaFolder type="large" media="video">video/</mediaFolder>

    <mediaFolder type="thumbnail" media="video">video/thumbs/</mediaFolder>

    <mediaFolder type="large" media="swf">video/</mediaFolder>

    <mediaFolder type="thumbnail" media="swf">video/thumbs/</mediaFolder>

    <mediaFolder type="large" media="image">wallimages/</mediaFolder>

    <mediaFolder type="thumbnail" media="image">wallimages/thumbs/</mediaFolder>

    </settings>';

    $res="select * from images";

    $red=mysql_query($res);

    $sr;

    while($abc=mysql_fetch_array($red)){

    $sr++;

    $id=$abc['id'];

    $image=$abc['image'];

    $main .='<item>

    <media:text>image</media:text>

    <media:content url="wallimages/" type="image/jpeg" width="800" height="600" />

    </item>';

    }

    include 'SimpleDOM.php';

    $xml_resource = new SimpleDOM('stuff.xml', 0, true);

    $xml_resource->content = '';

    $xml_resource->content->insertCDATA($main);

    $xml_resource->asXML('stuff.xml');


    --------------------------------------------------------------

    results is
    --------------------------------------------------------------



    <?xml version="1.0" encoding="UTF-8" standalone="no"?>

    <gallery>

    <content><![CDATA[<settings>

    <mediaFolder type="large" media="video">video/</mediaFolder>

    <mediaFolder type="thumbnail" media="video">video/thumbs/</mediaFolder>

    <mediaFolder type="large" media="swf">video/</mediaFolder>

    <mediaFolder type="thumbnail" media="swf">video/thumbs/</mediaFolder>

    <mediaFolder type="large" media="image">wallimages/</mediaFolder>

    <mediaFolder type="thumbnail" media="image">wallimages/thumbs/</mediaFolder>

    </settings><item>

    <media:text>image</media:text>

    <media:content url="wallimages/" type="image/jpeg" width="800" height="600" />

    </item>]]></content></gallery>


    ----------------------------------------------------------------------
    my problem is i dont not want the starting and ending tags...

    <![CDATA[ ]]>

    please give me the solution this is very urgently....
    Copy linkTweet thisAlerts:
    @wbxpertsauthorMar 06.2011 — my problem is:

    when i write the some data in to xml:<item></item>

    so xml change the <> tags to any other ords like:&ndt

    so i need these tags in xml when i insert php values in xml...

    tell me sir this is very important.....
    Copy linkTweet thisAlerts:
    @eval_BadCode_Mar 12.2011 — XML is plain text. You can write anything to a file and call it "XML", but XML itself has a very specific syntax. http://www.w3schools.com/xml/xml_syntax.asp

    You can write a normal file instead with an xml extension. SimpleDOM is going to enforce the XML syntax, to SimpleDOM it looks like you're trying to write 1 element, you will have to create new elements (INSIDE OF THE LOOP) if you want to use SimpleDOM instead of trying to write them all in 1 go.

    If you know what you're doing, just write a normal file. Don't mess with SimpleDOM.
    Copy linkTweet thisAlerts:
    @klanga2049Mar 13.2011 — CDATA is for character data, it's just text. If you want to append some XML, try appendXML(), e.g.

    [code=php]$xml_resource->content = '';
    $xml_resource->content->appendXML($main);[/code]


    Also, you haven't declared the "media" namespace, so you'll probably get an error for that.
    ×

    Success!

    Help @wbxperts spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.4,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,

    tipper: @Samric24,
    tipped: article
    amount: 1000 SATS,
    )...