2010-08-11 4 views
3

J'ai un fichier xmlpréserver le contenu HTML lors de l'analyse du fichier xml

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

J'utilise XPath et XQuery pour analyser ce fichier

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

mais iam obtenir que la chaîne. Comment puis-je conserver les balises HTML dans le résultat si iam tag parsing <value> Je reçois le contenu sans <ul> et <li> Comment puis-je obtenir le plein contenu html dans la balise <value> comme il est

Merci à l'avance

Répondre

0

utilisation asXML(), SimpleXMLElement :: asXML - Retourne une chaîne XML bien formé basée sur un élément SimpleXML

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

Je ne suis pas certain% 100 je suis votre problème, mais mon instinct me dit que vous êtes à la recherche d'une section 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> 

Ou vous pourriez échapper à tous ces < et > personnages:

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

lorsque j'ai essayé $ xml_str1 = file_get_contents ($ file); $ xml_str = $ xml_str. $ Xml_str1; $ xml = simplexml_load_string ($ xml_str); $ nodes = $ xml-> xpath ('// text'); reçois le résultat Tableau ( [0] => SimpleXMLElement Object ( [defalut] => Ce fichier présente les caractéristiques suivantes: [valeur] => SimpleXMLElement Object ( ) [Tittle] = > Lorem Ipsum ) ) – Warrior

+0

obtenir la valeur comme nulle – Warrior

+0

Quelle solution avez-vous essayé? –

0

Une fois que vous analysez le fichier, il est chargé en une représentation DOM, qui est un arbre en mémoire de tous les nœuds. Pour récupérer le HTML, vous devez sérialiser le nœud contenant le XML.

0

Pas besoin de CDATA On peut simplement utiliser

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

Il sera charger le contenu HTML tel quel

Questions connexes