Given an xml structure like this
<gesmes:Envelope>
<gesmes:subject>Reference rates</gesmes:subject>
<gesmes:Sender>
<gesmes:name>European Central Bank</gesmes:name>
</gesmes:Sender>
<Cube>
<Cube time="2010-03-26">
<Cube currency="USD" rate="1.3353"/>
<Cube currency="JPY" rate="124.00"/>
<Cube currency="BGN" rate="1.9558"/>
<Cube currency="CZK" rate="25.418"/>
...
...
</Cube>
</Cube>
</gesmes:Envelope>
how can i go about getting the values stored in to a hashmap or similar structure in php?
Have been trying to do this for the last few hours now but cant manage it 😀
It is homework so i guess no full solutuins please( tho the actual assignment is to use the web services, i am just stuck with parsing it 😀 ). Maybe someone could show me a brief example for a made up xml file that i could apply to mine?
Thanks
<gesmes:Envelope>is probably not the “whole truth”. A prefix likegesmes:needs to be associated with an uri. In your case it’s probablyAnd now you have to deal with namespaces….
There is php’s simplexml module. It has some shortcomings when namespaces are involved. But anyway… here it is:
prints
If you want to use XPath to find specific elements/nodes (either SimpleXMLElement::xpath or DOMXPath which you would use with DOMDocument as pointed out by cletus) you have to register the namespace first.
e.g.
prints
European Central Bank.And there are other functions/methods to parse an XML document. E.g. XMLReader or (which is probably the closest to your your original quest “how can i go about getting the values stored in to a hashmap […]?”) xml_parse_into_struct()