简体   繁体   中英

Getting values from XML file using simplexml_load_string

I have the following XML code

<epp:epp xmlns:epp="urn:ietf:params:xml:ns:epp-1.0">
  <epp:response>
    <epp:result code="1000">
      <epp:msg>Contact Info Command completed successfully</epp:msg>
    </epp:result>
    <epp:resData>
      <contact:infData xmlns:contact="urn:ietf:params:xml:ns:contact-1.0">
        <contact:id>con12</contact:id>
        <contact:status s="ok"/>
        <contact:postalInfo type="loc">
          <contact:name>Name</contact:name>
          <contact:org/>
          <contact:addr>
            <contact:street>streer</contact:street>
            <contact:street>street</contact:street>
            <contact:street/>
            <contact:city>City</contact:city>
            <contact:sp/>
            <contact:pc>186</contact:pc>
            <contact:cc>AA</contact:cc>
        </contact:addr>
      </contact:postalInfo>
        <contact:voice>0000000</contact:voice>
        <contact:fax>000000000</contact:fax>
        <contact:email>support@email.com</contact:email>
    </contact:infData>
    </epp:resData>

  </epp:response>
</epp:epp>

I'm having difficulties getting values from the XML string using simplexml_load_string

I can get the results in all node starting with epp: such as epp:msg uinsg the following code

$objects = simplexml_load_string($result);
$objects->children('epp', true)->response->result->msg;

I just cant seem to get the values below

<contact:infData xmlns:contact="urn:ietf:params:xml:ns:contact-1.0">
        <contact:id>con12</contact:id>
        <contact:status s="ok"/>
        <contact:postalInfo type="loc">
          <contact:name>Name</contact:name>
          <contact:org/>
          <contact:addr>
            <contact:street>streer</contact:street>
            <contact:street>street</contact:street>
            <contact:street/>
            <contact:city>City</contact:city>
            <contact:sp/>
            <contact:pc>186</contact:pc>
            <contact:cc>AA</contact:cc>
        </contact:addr>
      </contact:postalInfo>
        <contact:voice>0000000</contact:voice>
        <contact:fax>000000000</contact:fax>
        <contact:email>support@email.com</contact:email>
    </contact:infData>

I would also need to know how to get the code value in <epp:result code="1000">

Any help/pointers would be appreciated,

You're 99% of the way there (unless I'm missing something). Basically, whenever you want to "change" namespace, you use the ->children() or ->attributes() methods.

So you can say $objects->children('epp', true)->response->result->resData->children('contact', true)->infData->id

The code attribute in <epp:result code="1000"> is technically not in any namespace at all , so you have to select the NULL namespace to access it: $objects->children('epp', true)->response->result->attributes(NULL)->code .

Don't forget as well that by using the aliases ('epp', 'contact') you are reliant on the source of data not changing those, which they technically could without changing the "meaning" of the file. To guard against this, you have to hard-code the URIs ( urn:ietf:params:xml:ns:epp-1.0 and urn:ietf:params:xml:ns:contact-1.0 ) instead.

You were almost there. Access the deeper elements in a similar way:

$contact = $objects->children('epp', true)->response->resData->children('contact',tee);
$name = $contact->infData->postalInfo->name;

For the code value, I assume you mean <epp:result code="1000">

$resultAttributes = $objects->children('epp', true)->response->result->attributes();
$code = $resultAttributes['code'];

Note: both $name and $code end up being SimpleXMLElements not strings, but you can cast them to a string:

$a = (string)$name;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM