Save html content while parsing xml file

I have an 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 use 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 I only get the string. How can I save html tags as a result of if iam parsing <value>tag I get content without <ul>and <li>How to get full html content in a tag <value>, since it

Thanks in advance

+3
source share
5 answers

use asXML (), SimpleXMLElement :: asXML - return a correctly formed XML string based on the SimpleXML element

$result = $xml->xpath('//text');
echo $result[0]->asXML();
0
source
$nodes = $xml->xpath('//text/value');
+1
source

% 100, , , CDATA:

<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>

< >:

<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>
+1

DOM, . HTML , node, XML.

0

No need for CDATA. We can just use

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

It will load the HTML content as it

0
source

Source: https://habr.com/ru/post/1759164/


All Articles