Php 从XML文件中获取用于数据库插入的值

Php 从XML文件中获取用于数据库插入的值,php,simplexml,Php,Simplexml,我有一个以下格式的xml文件: <?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> </reco

我有一个以下格式的xml文件:

<?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>
再往下看:

<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>
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 )
我试过这样的方法:

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

model="test.pathology.category" id="icdcat1" 
对于在数据库中插入,我需要以下内容: 来自第1部分:

[id] => icdcat1 ) 
[field] => I Certain infectious and parasitic diseases 
id="icdcat1-1"
name = (A00-A09) Intestinal infectious diseases
ref="icdcat1" 2 etc...
第二部分:

[id] => icdcat1 ) 
[field] => I Certain infectious and parasitic diseases 
id="icdcat1-1"
name = (A00-A09) Intestinal infectious diseases
ref="icdcat1" 2 etc...
我很困惑,因为我是一个新手程序员。请求帮助。

请尝试此功能

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;     
}

您应该能够简单地迭代记录元素

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;
    }
}
属性(id)使用
[]
访问,字段元素的值使用
->

if部分检查是否存在第二个字段值,并从中提取ref元素

对于示例文件,这将输出

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

对数据进行编码/解码有什么意义?@u_mulder抱歉,正在测试。。帖子更新。你真正的问题是什么?@Philipp Maurer抱歉,它在顶部。我已经更新了帖子,现在已经在帖子末尾了。没有价值。我尝试过:foreach($xml as$syn){$w1=$syn->w1;$w2=$syn->w2;echo$w1;echo$w2;}simplexml_load_文件('syntest.xml')请尝试添加并解释代码的作用以及它如何解决问题。并正确格式化。@remiz_muhammed抱歉,您的代码没有任何作用。xml文件正在正确读取,我正在获取打印值。请将结果转储。非常感谢。我将尝试使代码适应我的需要。需要检查的一件事是,在字段中使用这些值时,将它们转换为字符串(例如,echo if
$record['id']
)。否则,它们将是SimpleXMLElement对象和函数不总是为您执行这种类型的强制转换(
echo
does)谢谢您提供的信息:)