tags:

views:

53

answers:

5

I have a xml file

<text>
    <defalut>This file has the following features:</defalut>
    <value>
        <ul>
            <li><info>CSS text formatting </info></li>
            <li><info>Text loaded from a XML</info></li>
            <li><info>Scrolls with easing</info></li>
            <li><info>Mouse wheel supported</info></li>
            <li><info>HTML supported</info></li>
            <li><info>Click on the bar to move  the handle to that point</info></li>
            <li><info>Supports images</info></li>
            <li><info>The scrollbar hides if not needed</info></li>
            <li><info>The scrollbar resizes proportonal to the text sizeq</info></li>
        </ul>
    </value>
    <tittle>Lorem Ipsum</tittle>
</text>

I am using xpath and xquery to parse this file

$xml_str1 = file_get_contents("file.xml");
echo $xml_str = $xml_str.$xml_str1;
$xml = simplexml_load_string($xml_str);
$nodes = $xml->xpath('//text');

but iam getting the string only. How can i preserve the html tags in the result if iam parsing <value> tag i am getting the content without <ul> and <li> How can i get the full html content in the <value> tag as it is

Thanks in advance

+1  A: 

$nodes = $xml->xpath('//text/value');

+1  A: 

I'm not %100 certain I follow your problem, but my gut tells me that you're looking for a CDATA section:

<text>
    <defalut>This file has the following features:</defalut>
    <value>
    <![CDATA[
        <ul>
            <li><info>CSS text formatting </info></li>
            <li><info>Text loaded from a XML</info></li>
            <li><info>Scrolls with easing</info></li>
            <li><info>Mouse wheel supported</info></li>
            <li><info>HTML supported</info></li>
            <li><info>Click on the bar to move  the handle to that point</info></li>
            <li><info>Supports images</info></li>
            <li><info>The scrollbar hides if not needed</info></li>
            <li><info>The scrollbar resizes proportonal to the text size</info></li>
        </ul>
    ]]>
    </value>
    <tittle>Lorem Ipsum</tittle>
</text>

Or you could escape all those < and > characters:

<text>
    <defalut>This file has the following features:</defalut>
    <value>
        &lt;ul&gt;
            &lt;li&gt;&lt;info&gt;CSS text formatting &lt;/info&gt;&lt;/li&gt;
            &lt;li&gt;&lt;info&gt;Text loaded from a XML&lt;/info&gt;&lt;/li&gt;
            &lt;li&gt;&lt;info&gt;Scrolls with easing&lt;/info&gt;&lt;/li&gt;
            &lt;li&gt;&lt;info&gt;Mouse wheel supported&lt;/info&gt;&lt;/li&gt;
            &lt;li&gt;&lt;info&gt;HTML supported&lt;/info&gt;&lt;/li&gt;
            &lt;li&gt;&lt;info&gt;Click on the bar to move  the handle to that point&lt;/info&gt;&lt;/li&gt;
            &lt;li&gt;&lt;info&gt;Supports images&lt;/info&gt;&lt;/li&gt;
            &lt;li&gt;&lt;info&gt;The scrollbar hides if not needed&lt;/info&gt;&lt;/li&gt;
            &lt;li&gt;&lt;info&gt;The scrollbar resizes proportonal to the text size&lt;/info&gt;&lt;/li&gt;
        &lt;/ul&gt;
    </value>
    <tittle>Lorem Ipsum</tittle>
</text>
LeguRi
when i tried $xml_str1 = file_get_contents($file);$xml_str = $xml_str.$xml_str1;$xml = simplexml_load_string($xml_str);$nodes = $xml->xpath('//text');am getting the resultArray( [0] => SimpleXMLElement Object ( [defalut] => This file has the following features: [value] => SimpleXMLElement Object ( ) [tittle] => Lorem Ipsum ))
THOmas
am getting value as null
THOmas
Which solution did you try?
LeguRi
I used first solution with CDATA
THOmas
A: 

Once you parse the file, it gets loaded into a DOM representation, which is an in-memory tree of all the nodes. To get the HTML back you have to serialize the node containing the XML.

Jim Garrison
A: 

use asXML(), SimpleXMLElement::asXML — Return a well-formed XML string based on SimpleXML element

$result = $xml->xpath('//text');
echo $result[0]->asXML();
Andy Lin
A: 

No need of CDATA WE can simply use

$result = $xml->xpath('//text');
echo $result[0]->asXML();

It will load the HTML content as it is

THOmas