php get xml inner tag content
989
I need to perform an API that it's response is a XML. But the XML values are inside the XML tags.
For example
<example>
<item productid = "1" productname = "xxx" cost = "5.3"/>
<item productid = "2" productname = "yyy" cost = "4.0"/>
<item productid = "3" productname = "zzz" cost = "1.75"/>
</example>
Can anyone tell me how can i, move between tags and get elemnt values For example:
example:
item:
productid -> 1,
productname -> xxx,
cost -> 5.3
item:
productid -> 2,
productname -> yyy,
cost -> 4.0
item:
productid -> 3,
productname -> zzz,
cost -> 1.75
Thanx
Answer
Solution:
XPath: http://php.net/manual/en/simplexmlelement.xpath.php
Not mandatory, you can just get all the children and loop through them, but XPath is more versatile in real world scenarios ( where you have multiple levels of XML nodes ) and is considerably more readable.
Answer
Solution:
Or you could use DOMElement
Answer
Solution: