开发者

How read < abc: xyz > xml tag using php?

开发者 https://www.devze.com 2023-01-04 18:49 出处:网络
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..

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->i开发者_Python百科tem(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


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.


you need DOMDocument::getElementsByTagNameNS


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消