riad
riad

Reputation: 7184

parse xml data using php

Dear all ,I have the sample files below.I can parse data from the simple xml tag.But how can i parse data from this type of xml tag?< p:price >test data< /p:price> or < p1:translation lang="en" type="text" >pls guide..

sample xml file

<?xml version="1.0" encoding="UTF-8"?>
    <mynotes>
   <note>
     <tasks>Task 1</tasks>
     <details>Detail 1</details>
   </note>
   <note>
     <tasks>Task2</tasks>
     <details> Detail 2</details>   
   </note>
      <test>test data</test>
    </mynotes>

php code...

$objDOM = new DOMDocument();
$objDOM->load("abc.xml"); //make sure path is correct
$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>";
  }

  $test = $objDOM->getElementsByTagName("test");
  $testValue  = $test->item(0)->nodeValue;
  echo $testValue;

i cannot parse data from < p1:translation lang="en" type="text" > this type of tag..pls guide me how can i parse data from this type of tag or < category:applicationCategory > this type of xml tag....

thanks
riad

Upvotes: 2

Views: 556

Answers (1)

user268396
user268396

Reputation: 11966

You need to query by namespace as well as tagname. There is the getElementsByTagnameNS method for that: http://www.php.net/manual/en/domdocument.getelementsbytagnamens.php

Upvotes: 1

Related Questions