druhoytza979667566 2017-11-27 11:10
浏览 32
已采纳

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

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,"\"
";
}

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.

  • 写回答

2条回答 默认 最新

  • douaipi3965 2017-11-27 11:48
    关注

    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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘