Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 691165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:30:42+00:00 2026-05-14T02:30:42+00:00

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>

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T02:30:43+00:00Added an answer on May 14, 2026 at 2:30 am

    <gesmes:Envelope> is probably not the “whole truth”. A prefix like gesmes: needs to be associated with an uri. In your case it’s probably

    <gesmes:Envelope
      xmlns:gesmes="http://www.gesmes.org/xml/2002-08-01"
      xmlns="http://www.ecb.int/vocabulary/2002-08-01/eurofxref"
    >
    

    And 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:

    $s = new SimpleXMLelement(getData());
    foreach( $s->Cube as $cubeContainer) {
      foreach( $cubeContainer as $cubeEntry) {
        echo 'time: ', $cubeEntry['time'], "\n";
        foreach( $cubeEntry->Cube as $cubeElement) {  
          echo '  ', $cubeElement['currency'], '=', $cubeElement['rate'], "\n";
        }
      }
    }
    
    // now that was easy ...now the fun of having namespaces/prefixes
    foreach( $s->children("http://www.gesmes.org/xml/2002-08-01")->subject as $subject) {
      echo 'subject: ', $subject, "\n";
    }
    
    foreach( $s->children("http://www.gesmes.org/xml/2002-08-01")->Sender as $sender) {
      foreach( $sender->children("http://www.gesmes.org/xml/2002-08-01")->name as $name) {
        echo 'sender/name: ', $name, "\n";
      }
    }
    
    
    
    function getData() {
      return '<gesmes:Envelope xmlns:gesmes="http://www.gesmes.org/xml/2002-08-01" xmlns="http://www.ecb.int/vocabulary/2002-08-01/eurofxref">
        <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>';
    }
    

    prints

    time: 2010-03-26
      USD=1.3353
      JPY=124.00
      BGN=1.9558
      CZK=25.418
    subject: Reference rates
    sender/name: European Central Bank
    

    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.

    $s = new SimpleXMLelement(getData());
    $s->registerXPathNamespace('gesmes', 'http://www.gesmes.org/xml/2002-08-01');
    foreach($s->xpath('//gesmes:name[position()=1]') as $n){
      echo $n, "\n";
    }
    

    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()

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given an XML structure like this: <garage> <car>Firebird</car> <car>Altima</car> <car>Prius</car> </garage> I want to
Given this XML: <?xml version=1.0 encoding=utf-8?> <content dataType=XML> <item type=Promotion name=Sample Promotion expires=04/01/2009> <![CDATA[
Given an XML document like this: <!DOCTYPE doc SYSTEM 'http://www.blabla.com/mydoc.dtd'> <author>john</author> <doc> <title>&title;</title> </doc>
given an xml string like this: <some><nested><xml>value</xml></nested></some> what's the best option (using ruby) to
I have an xml mapping file that looks something like this <colourMappings> <model name=modelX>
I have an xml file nodes structure like this <Employee> <EmpId></EmpId> <EmpName></EmpName> <Salary> <Basic></Basic>
Having a XML-structure like this: <ContextDoc> <PurchaseOrder xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xmlns:xsd=http://www.w3.org/2001/XMLSchema> <OrderForms> <OrderForm> <Shipments> <Shipment> ...
I have a given xml structured like this: <elem> <val id=1>aaa</val> <val id=2>bbb</val> </elem>
I'm working with a XML structure that looks like this: <ROOT> <ELEM_A> <A_DATE>20100825</A_DATE> <A_TIME>141500</A_TIME>
I have a given XML document (structure can not be changed) and want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.