-1

Possible Duplicate:
Parsing XML into Array

how convert my xml file to php array, this is my xml file:

<?xml version="1.0" standalone="yes"?>
<EKG_DS>
  <EKG_PASIEN>
    <DOKTER>1</DOKTER>
    <NO_RM>0007</NO_RM>
    <NAMA>DONI</NAMA>
    <UMUR>36</UMUR>
    <ALAMAT>JAKARTA</ALAMAT>
    <TANGGAL>28/01/2013</TANGGAL>
  </EKG_PASIEN>
  <EKG>
    <ID_IRAMA_DSR>2</ID_IRAMA_DSR>
    <DENYUT_JNTG>2</DENYUT_JNTG>
    <ID_REG_DENYUT_JNTG>2</ID_REG_DENYUT_JNTG>
    <ID_GLMBG_P>2</ID_GLMBG_P>
    <ID_BENTUK_GLMBG_P>2</ID_BENTUK_GLMBG_P>
    <ID_REG_GLMBG_P>1</ID_REG_GLMBG_P>
  </EKG>
</EKG_DS>

i try use this code but just string... i need an array..

if (file_exists('C:/download/171_EKG.xml')) {
    $xml = simplexml_load_file('C:/download/171_EKG.xml');
     print"<pre>";
    print_r($xml);
}

please help me...

2
  • You want the file as a string, or the data as an array? Commented Jan 29, 2013 at 5:17
  • i need data as an array. Commented Jan 29, 2013 at 5:20

3 Answers 3

0

Try this:

$xml2Array = array();
if (file_exists('C:/download/171_EKG.xml')) {
    $xml = simplexml_load_file('C:/download/171_EKG.xml');
    foreach( $xml->children() as $key => $value ) {
        $nodes = array();
        foreach( $value->children() as $k => $v ) {
            $nodes[ ( string ) $v->getName() ] = ( string ) $v;
        }

        $node = ( string ) $value->getName();
        $xml2Array[ $node ][] = $nodes;
    }
}
print_r($xml2Array);

Hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

0

Use the xpath command, it works great in creating an array.

For example, to create array of all Tags named: A

$xml = new SimpleXMLElement("NameOfTheXMLFiles"); $arr=$xml->xpath("A");

Then fetch any alement with tag A as an array: $arr[0], $arr[1], etc..

1 Comment

could you tell me with full code... i am new in php.
0

I have tried the same code and it returned an perfect array of object, which is obvious.

Here is what I have tried

if (file_exists('test.xml')) {
    $xml = simplexml_load_file('test.xml');
    print_r($xml);
    $xml->EKG_PASIEN->ALAMAT; //Returns JAKARTA
}

In above code you can also find how to access a single record from objects array.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.