2

Using my below code i can read <abcxyz> xml tag easily. but how can i read the data between <abc:xyz> </abc:xml> xml tag.. xml tag using php.pls help....

my php sample code...

 $objDOM->load("abc.xml"); 
  $note = $objDOM->getElementsByTagName("note");  
   foreach( $note as $value )
   {
    $tasks = $value->getElementsByTagName("tasks");
    $task  = $tasks->item(0)->nodeValue;
    $details = $value->getElementsByTagName("details");
    $detail  = $details->item(0)->nodeValue;    
    echo "$task :: $detail<br>";
   }

My XML sample code:

<mynotes>
     <note>
        <tasks>Task 1</tasks>
        <details>Detail 1</details>
     </note>
     <abc:xyz> Cannot Read the XML data between this tag</abc:xyz>
 </mynotes>

Pls guide me...

Thanks
Riad

Damien_The_Unbeliever
  • 234,701
  • 27
  • 340
  • 448
riad
  • 7,144
  • 22
  • 59
  • 70
  • 2
    What's the class of $objDOM? Can it do XML Namespaces? Because that looks like what `abc` is in the context of the tag... – Weston C Jun 28 '10 at 07:07

3 Answers3

7

abc:xyz means that the element is named xyz, and the namespace is indicated by abc. The namespace part is actually shorthand for an URI, which is usually also given in the XML file. For example, you may see this:

xmlns:abc="http://www.abc.com/xml"

In this case, elements which have abc before the colon are in the namespace http://www.abc.com/xml.

To retrieve this element, you need to use getElementsByTagNameNS and pass http://www.abc.com/xml as the namespace.

Sjoerd
  • 74,049
  • 16
  • 131
  • 175
  • 1
    Please check if you can answer this http://stackoverflow.com/questions/6665222/unable-to-parse-xml-data-with-colon-from-response-using-getnamespaces – Sandeepan Nath Jul 12 '11 at 13:53
2

you need DOMDocument::getElementsByTagNameNS

Sergey Eremin
  • 10,994
  • 2
  • 38
  • 44
  • 1
    could you pls give me an example..because on that link i don't get any proper example.. – riad Jun 28 '10 at 07:11
  • 1
    using this example i get the abc and xyz into a seperated variable like $v1 and $v2.But i cannot put $value->getElementsByTagName($v1,$v2); on this function to get value between this..I need the value between this tag – riad Jun 28 '10 at 07:15
1

Going with the DOMDocument::getElementsByTagNameNS way like others have suggested, here is a working code (including reading the inner content), assuming that you also have some namespace declaration (like <abc:response xmlns:abc="http://api-url"> ) part as pointed out by @Sjoerd -

$xml = '<?xml version="1.0"?>
         <abc:response xmlns:abc="http://api-url">
         <mynotes>
          <note>
            <tasks>Task 1</tasks>
            <details>Detail 1</details>
          </note>
         <abc:xyz> Can Read the XML data between this tag!!</abc:xyz>
        </mynotes>
        </abc:response>';

$dom = new DOMDocument;
// load the XML string defined above
$dom->loadXML($xml);


foreach ($dom->getElementsByTagNameNS('http://api-url', '*') as $element) 
{
  //echo 'see - local name: ', $element->localName, ', prefix: ', $element->prefix, "\n";
  if($element->localName == "xyz")
      echo get_inner_html($element);

}

function get_inner_html( $node ) 
{ 
    $innerHTML= ''; 
    $children = $node->childNodes; 
    foreach ($children as $child) 
    { 
        $innerHTML .= $child->ownerDocument->saveXML( $child ); 
    } 

    return $innerHTML;  
}

Here is a working link showing the output.

Note that I have just wrapped your xml inside this -

   '<?xml version="1.0"?>
         <abc:response xmlns:abc="http://api-url">'
              .$yourxml
         .'</abc:response>';

I used the solution I got from here PHP DOM get nodevalue html? (without stripping tags)... was stuck with a similar problem these days.

Community
  • 1
  • 1
Sandeepan Nath
  • 9,966
  • 17
  • 86
  • 144