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

1
  • 2
    What's the class of $objDOM? Can it do XML Namespaces? Because that looks like what abc is in the context of the <abc:xyz> tag... Commented Jun 28, 2010 at 7:07

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Please check if you can answer this stackoverflow.com/questions/6665222/…
2

you need DOMDocument::getElementsByTagNameNS

2 Comments

could you pls give me an example..because on that link i don't get any proper example..
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
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.