简体   繁体   中英

Obtaining values from an XML file for database insert

I have an xml file of following format:

<?xml version="1.0"?>
<tryton>
<data skiptest="1" noupdate="1">
<record model="test.pathology.category" id="icdcat1">
<field name="name">I Certain infectious and parasitic diseases</field>
</record>

<record model="test.pathology.category" id="icdcat2">
<field name="name">II Neoplasms</field>
</record>

and further down:

<record model="test.pathology.category" id="icdcat1-1">
<field name="name">(A00-A09) Intestinal infectious diseases</field>
<field name="parent" ref="icdcat1"></field>
</record>

<record model="test.pathology.category" id="icdcat2-6">
<field name="name">(C45-C49) Malignant neoplasms of mesothelial and soft tissue</field>
<field name="parent" ref="icdcat2"></field>
</record>
</data>
</tryton>

I tried the following:

<?php
if(file_exists('xml/myfile.xml'))
   {
$xml = simplexml_load_file('xml/myfile.xml', 'SimpleXMLElement', LIBXML_NOCDATA);
}
print_r($xml);
?>

print_r gives the following values:

 SimpleXMLElement Object ( [data] => SimpleXMLElement Object ( [@attributes] => Array ( [skiptest] => 1 [noupdate] => 1 ) [comment] => SimpleXMLElement Object ( ) [record] => Array ( 
[0] => SimpleXMLElement Object ( [@attributes] => Array ( [model] => test.pathology.category [id] => icdcat1 ) [field] => I Certain infectious and parasitic diseases ) 
[1] => SimpleXMLElement Object ( [@attributes] => Array ( [model] => test.pathology.category [id] => icdcat2 ) [field] => II Neoplasms )

and further down:

SimpleXMLElement Object ( [@attributes] => Array ( [model] => test.pathology.category [id] => icdcat1-1 ) [field] => Array ( 
[0] => (A00-A09) Intestinal infectious diseases 
[1] => SimpleXMLElement Object ( [@attributes] => Array ( [name] => parent [ref] => icdcat1 ) ) ) ) [23] => SimpleXMLElement Object ( [@attributes] => Array ( [model] => test.pathology.category [id] => icdcat1-2 )

I tried something like:

foreach($xml->data->record[0]->attributes() as $a => $b) {
    echo $a,'="',$b,"\"\n";
}

and i am getting :

model="test.pathology.category" id="icdcat1" 

For insert in database, i need the following: From part 1:

[id] => icdcat1 ) 
[field] => I Certain infectious and parasitic diseases 

and in second part:

id="icdcat1-1"
name = (A00-A09) Intestinal infectious diseases
ref="icdcat1" 2 etc...

Confused as i am a newbie programmer. Help requested.

You should be able to simply iterate over the record elements...

foreach($xml->data->record as $record) {
    echo (string)$record['id']."/".(string)$record->field[0].PHP_EOL;
    if ( count($record->field) > 1 )    {
        echo $record->field[1]['ref'].PHP_EOL;
    }
}

The attribute (id) is accessed using [] and the value of the field element uses -> .

The if part checks if there is a second field value and extracts the ref element from it.

With the sample file, this outputs...

icdcat1/I Certain infectious and parasitic diseases
icdcat2/II Neoplasms
icdcat1-1/(A00-A09) Intestinal infectious diseases
icdcat1
icdcat2-6/(C45-C49) Malignant neoplasms of mesothelial and soft tissue
icdcat2

Pls Try This

if(!$xml=simplexml_load_file('syntest.xml')){
trigger_error('Error reading XML file',E_USER_ERROR);
}



foreach ($xml as $syn) {
$w1 = $syn->w1;  
$w2 = $syn->w2;     
}

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