1

below is part of my XML where I try to get data from, basicly I need to insert them to array where "role" is key and "entry" is value.

Here is XML:

<CommunicationDetailList>
  <CommunicationDetail>
    <Role>Phone1</Role>
    <Entry>727831333</Entry>
  </CommunicationDetail>
  <CommunicationDetail>
    <Role>Mobile</Role>
    <Entry>727834125</Entry>
  </CommunicationDetail>
  <CommunicationDetail>
    <Role>Fax1</Role>
    <Entry>123456789</Entry>
  </CommunicationDetail>
  <CommunicationDetail>
    <Role>EMail1</Role>
    <Entry>[email protected]</Entry>
 </CommunicationDetail>                             
</CommunicationDetailList>              

This is my PHP code, unfotunately it doesn't work correctly (add just first one not rest of it, so I have access just to Phone1):

        //this is somewhere on top of my code
        $doc = new DOMDocument();

        //Load XML to DOM
        $doc->loadXml($xml);
        .
        .
         // here I parse rest of XML, where `<tags>` are unique
        .
        .
        //and here is that important part
        $communicationDetails = $doc->getElementsByTagName( "CommunicationDetailList" );
        foreach( $communicationDetails as $detail )
        {  
          $keys = $detail->getElementsByTagName( "Role" );
          $key = $keys->item(0)->nodeValue;

          $values = $detail->getElementsByTagName( "Entry" );
          $value = $values->item(0)->nodeValue;

          //adding login and password to array              
          $data[$key] = $value;

        }   

Can someone help me to access to this XML

2
  • "unfotunately it doesn't work correctly" And why doesn't it work correctly? What errors are you getting? Have you done some debugging to locate where the error is occurring? Commented Aug 24, 2015 at 10:19
  • Hi @Epodax I belieave we just miss each other, I was editing question , answer on it is in bracket, so : No error but just first key + value is added to array. Commented Aug 24, 2015 at 10:21

3 Answers 3

3

Try using SimpleXMLElement like this

<?php

$xml = 'data.xml';
//load xml from file
$doc = simplexml_load_file($xml);

// or load from string
// $doc = simplexml_load_string($xmlString);

foreach($doc->CommunicationDetail as $detail){

    //print $detail->Role . ' - ' . $detail->Entry . PHP_EOL;

    $data[(string)$detail->Role] = (string)$detail->Entry;
    // we cast the xml elements as strings to be used as keys and values in the array
}

print_r($data);

//output is 
Array
(
    [Phone1] => 727831333
    [Mobile] => 727834125
    [Fax1] => 123456789
    [EMail1] => [email protected]
)
Sign up to request clarification or add additional context in comments.

1 Comment

Hey @Alex Andrei, thanks for advise, I give you thump up for this nice solution. However I need to use DOM for some other purposes.
1

Try this may be it help

foreach( $communicationDetails as $detail )
{  
    $keys = $detail->getElementsByTagName( "Role" );
    $values = $detail->getElementsByTagName( "Entry" );
    $length = $keys->length;
        for($i = 0; $i <= $length; $i++)
        {
            $key = $keys->item($i)->nodeValue;
            $value = $values->item($i)->nodeValue;
            $data[$key] = $value;
        }
}

Comments

0

The problem is with

$item(0)

If you were to use a iterated loop like

 for ($i=0; $i<count($keys); $i++) { echo $keys[$i]; }

Then it would go through the entire array.

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.