简体   繁体   English

从数据库中的xml插入子元素值作为json

[英]Insert childelement values as json from xml in database

I want to insert childelement values as json from xml in database.我想将子元素值作为 json 从数据库中的 xml 插入。

My xml我的 xml

<Listings>
   <Listing>
      <ParentElement>
         ParentValue
      </ParentElement>

      <ParentElement>
         <ChildElement>
             ChildValue
         </ChildElement>
         <ChildElement>
             ChildValue
         </ChildElement>
     </ParentElement>
   </Listing>
</Listings>

I can get the values in doing loop by我可以通过循环获取值

foreach($xml->xpath('//ChildElement/*') as $child) {
    echo $child;
}

How can I insert the values of child in my database as json?如何将 child 的值作为 json 插入到我的数据库中?

Most of this time might be spend for database communication.大部分时间可能用于数据库通信。 Try using a buffer and save multiple records at once using mass insert statements.尝试使用缓冲区并使用批量插入语句一次保存多条记录。

INSERT INTO table_name (field_name, ...) VALUES (value_1_1, ...), (value_2_1, ...), ...

Or convert the XML into a simplified structure ( XML or CSV ) and use the database import.或者将 XML 转换为简化结构( XMLCSV )并使用数据库导入。

LOAD XML INFILE '/path/records.xml' INTO TABLE table_name
LOAD DATA INFILE '/path/records.csv' INTO TABLE table_name

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM