2

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

1 Answer 1

1

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

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

2 Comments

sorry ..i don't get u..i have a tag like this...< p:price >100 dollar < /p:price>..how i can retrieve the info between this tag..pls guide..on your provided example just showing the separated tag name like p and price..but i need the between info...
That <prefix:name> syntax is shorthand for writing <my-namespace:name>, so you would query by the namespace that is bound to the p or p1 prefix. E.g. if p is declared like this: xmlns:p="test" it means that to query for p:price is equivalent to query for price elements in the "test" namespace. So in that case you'd supply the "test" namespace. In other words: ignore the "p" prefix, use its namespace directly.

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.