Jump to content

Reading XML with elements and Attributes


bijukon

Recommended Posts

Hi all

 

I'm using simplexml to read xml files in to my database, normally getting xml data is pretty straightforward.  I'm having trouble however trying to get data for when it is formatted in the following way:

 

 

<comments>

<comment lang="fr_FR"><![CDATA[lalalalalalalalalalallala]]></comment>

<comment lang="en_GB"><![CDATA[hello english descriptions]]></comment>

<comment lang="de_DE"><![CDATA[yayayayayayay]]></comment>

<comment lang="it_iT"><![CDATA[]]></comment>

<comment lang="es_ES"><![CDATA[]]></comment>

<comment lang="ru_RU"><![CDATA[]]></comment>

<comment lang="no_NO"><![CDATA[]]></comment>

</comments>

 

 

I normally extract data $variable = $advert->reference. 

 

I'm having trouble extracting en_gb data.  Any guidance would be appreciated.

 

Thanks in advance.

 

 

You have to use a combination of the DOM element and XPath.  Here is an example using your test data to extract all of the comment values that have a lang attribute value of en_GB.

 

$dom = new DOMDocument;
$dom->load('comment.xml');
$xpath = new DOMXPath($dom);
$xpath->registerNamespace("php", "http://php.net/xpath");
$nodes = $xpath->query('/comments/comment[@lang = "en_GB"]');

foreach ($nodes as $node) 
{
   $value  = $node->nodeValue;
   echo $value;
}
?>

 

This assumes the XML file is in the same directory as the PHP script.  Let me know if you have any questions.

Here is a simplexml example to extract all, of course you could add a if statement to find the "en_GB"..  :shrug:

 

$XMLData = <<<XML
      <comments>
         <comment lang="fr_FR"><![CDATA[lalalalalalalalalalallala]]></comment>
         <comment lang="en_GB"><![CDATA[hello english descriptions]]></comment>
         <comment lang="de_DE"><![CDATA[yayayayayayay]]></comment>
         <comment lang="it_iT"><![CDATA[]]></comment>
         <comment lang="es_ES"><![CDATA[]]></comment>
         <comment lang="ru_RU"><![CDATA[]]></comment>
         <comment lang="no_NO"><![CDATA[]]></comment>
      </comments>
XML;
$XML = simplexml_load_string($XMLData);
foreach($XML->comment as $element) {
    $attr = $element->attributes();
    echo "ATTR:".$attr['lang']."<BR />\n";
    echo "VALUE:$element<BR />\n";
    echo "<hr>";
}

 

Hope that helps

Thank you very much Maq & MadTechie

 

I've now managed to extract the data as mentioned, I'm using the Medtechie's suggestions as that works with my script without major rewrite, but thanks for the heads up on the Dom Xpath method, I'll certainly remember to consider using it.

 

Thanks again folks.

 

Thank you very much Maq & MadTechie

 

I've now managed to extract the data as mentioned, I'm using the Medtechie's suggestions as that works with my script without major rewrite, but thanks for the heads up on the Dom Xpath method, I'll certainly remember to consider using it.

 

Thanks again folks.

 

 

Good to hear you you got it working.  You can mark the topic as solved by clicking the tab on the bottom left.

It's also perfectly possible to take the XPath idea and use it with SimpleXML like:

 

$xml  = <<<XML
      <comments>
         <comment lang="fr_FR"><![CDATA[lalalalalalalalalalallala]]></comment>
         <comment lang="en_GB"><![CDATA[hello english descriptions]]></comment>
         <comment lang="de_DE"><![CDATA[yayayayayayay]]></comment>
         <comment lang="it_iT"><![CDATA[]]></comment>
         <comment lang="es_ES"><![CDATA[]]></comment>
         <comment lang="ru_RU"><![CDATA[]]></comment>
         <comment lang="no_NO"><![CDATA[]]></comment>
      </comments>
XML;
$sxml  = simplexml_load_string($xml);
$lang  = 'en_GB';
$nodes = $sxml->xpath("/comments/comment[@lang='$lang']");
echo "Comment for lang '$lang' is: $nodes[0]";

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.