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://www.us@here.co.uk</website>
    <email>special me@here.co.uk</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
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://www.us@here.co.uk</website>
    <email>special me@here.co.uk</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;
?>

Link to comment
Share on other sites

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. :-\

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.