Jump to content

getElementsByTagName xml, php5 et al


GURAQTINVU

Recommended Posts

OK, so I have my xml file:

 

<?xml version="1.0" encoding="utf-8"?>
<details>
  <nameone>
    <paras>1</paras>
    <paras><em>2</em></paras>
    <paras>3</paras>
  </nameone>
  <nametwo>
    <paras>1</paras>
    <paras>2</paras>
    <paras><em>3</em></paras>
    <website>http://[email protected]</website>
    <email>special [email protected]</email>
  </nametwo><
</details>

 

I can do what I want with:

 

$xml = simplexml_load_file($file);

$myVar = "";

foreach ($xml->nameone->para as $para) {$myVar .= "<p>".$para."</p>";}

echo $myVar

 

for example, but this doesn't allow for nested 'ad lib' tags (please see my cunning nested <em>).

 

What I need is a way of requesting all nodes and values beneath a node, <nameone> for instance, so that all nodes beneath are just included ver batum.  Does anyone know an easy and neat way - I'd be grateful.

Link to comment
https://forums.phpfreaks.com/topic/136682-getelementsbytagname-xml-php5-et-al/
Share on other sites

xpath is amazing :)

 

<?php
$xml = <<<XML
<details>
  <nameone>
    <paras>1</paras>
    <paras><em>2</em></paras>
    <paras>3</paras>
  </nameone>
  <nametwo>
    <paras>1</paras>
    <paras>2</paras>
    <paras><em>3</em></paras>
    <website>http://[email protected]</website>
    <email>special [email protected]</email>
  </nametwo>
</details>
XML;

$xml = simplexml_load_string($xml);
$eles = $xml->xpath('/details/child::*/paras');
$myVar = '';
foreach($eles as $para){
  $myVar .= "<p>".$para."</p>";
}
echo $myVar;
?>

Thanks, you are right about xpath but I seem to struggle at every turn as I am now left with an array of SimpleXMLElement objects (which exactly defines the xml I want apart from the fact that it returns a array with only <em> having an associative index - em) and cannot find a way to turn that array into a string of tags and text. :-\

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.