0

I am trying to getting attributes of product(despecid) node using php function using simplexml_load_string and SimpleXMLElement of following xml.

<productlist brand="HP" model="Color Laserjet 3000">
   <product despecid="CAN21679">IR C1021/MF8450 Fuser Unit (Fixing Assembly)</product>
   <product despecid="HPRM1-2743-180CN">CLJ 3000/3600/3800 CP3505 fuser unit</product>
   <product despecid="XER003R99755">Xerox XRC toner Q7560A black</product>  
</productlist>

but i got out of both ( simplexml_load_string and SimpleXMLElement) method output as below.

object(SimpleXMLElement)#179 (2) {
["@attributes"]=>
 array(2) {
  ["Brand"]=>
    string(2) "HP"
  ["Model"]=>
    string(19) "Color Laserjet 3000"
 }
 ["Product"]=>
   array(6) {
    [0]=>
     string(44) "IR C1021/MF8450 Fuser Unit (Fixing Assembly)"
    [1]=>
     string(36) "CLJ 3000/3600/3800 CP3505 fuser unit"
    [2]=>
      string(28) "Xerox XRC toner Q7560A black"
    [3]=>
      string(27) "Xerox XRC toner Q7561A cyan"
    [4]=>
     string(29) "Xerox XRC toner Q7562A yellow"
    [5]=>
     string(30) "Xerox XRC toner Q7563A magenta"
   }
}

Is there any method to get attribute despecid of product. Please help...

1 Answer 1

3
$xml = <<<XML
<productlist brand="HP" model="Color Laserjet 3000">
   <product despecid="CAN21679">IR C1021/MF8450 Fuser Unit (Fixing Assembly)</product>
   <product despecid="HPRM1-2743-180CN">CLJ 3000/3600/3800 CP3505 fuser unit</product>
   <product despecid="XER003R99755">Xerox XRC toner Q7560A black</product>  
</productlist>
XML;


foreach (simplexml_load_string($xml)->product as $product) {
   print_r($product->attributes()->despecid); 
}

See Demo

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

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.