简体   繁体   中英

Insert childelement values as json from xml in database

I want to insert childelement values as json from xml in database.

My 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?

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.

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

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