简体   繁体   English

将 XML 数据存储到数据库中的最佳方式

[英]Best way to store XML data into database

I have XML file with multiple level of children.我有多个级别的子级的 XML 文件。 Which is best way to store that data into database?将数据存储到数据库中的最佳方法是什么? To make different table for node and save data with relationship or?为节点制作不同的表并使用关系保存数据? What to do if we have 50 different nodes ?如果我们有 50 个不同的节点怎么办?

Similar XML类似的 XML

<storage>
   <product description="Cardigan Sweater" product_image="cardigan.jpg">
      <storage_item gender="Men's">
         <item_number>QWZ5671</item_number>
         <price>39.95</price>
         <size description="Medium">
            <color image="red_cardigan.jpg">Red</color>
            <color image="burgundy_cardigan.jpg">Burgundy</color>
         </size>
         <size description="Large">
            <color image="red_cardigan.jpg">Red</color>
            <color image="burgundy_cardigan.jpg">Burgundy</color>
         </size>
      </storage_item>
      <storage_item gender="Women's">
         <item_number>RRX9856</item_number>
         <price>42.50</price>
         <size description="Small">
            <color image="red_cardigan.jpg">Red</color>
            <color image="navy_cardigan.jpg">Navy</color>
            <color image="burgundy_cardigan.jpg">Burgundy</color>
         </size>
         <size description="Medium">
            <color image="red_cardigan.jpg">Red</color>
            <color image="navy_cardigan.jpg">Navy</color>
            <color image="burgundy_cardigan.jpg">Burgundy</color>
            <color image="black_cardigan.jpg">Black</color>
         </size>
         <size description="Large">
            <color image="navy_cardigan.jpg">Navy</color>
            <color image="black_cardigan.jpg">Black</color>
         </size>
         <size description="Extra Large">
            <color image="burgundy_cardigan.jpg">Burgundy</color>
            <color image="black_cardigan.jpg">Black</color>
         </size>
      </storage_item>
   </product>
</storage>

You can convert this XML to JSON format and save it in a NoSQL database like MongoDB.您可以将此 XML 转换为 JSON 格式并将其保存在 NoSQL 数据库(如 MongoDB)中。

UPDATE : If you need to save it into a SQL Database I think you would need to use JAXB or XMLBeans to convert to java objects and then use JDBC or JPA to save it into the DB.更新:如果您需要将其保存到 SQL 数据库中,我认为您需要使用 JAXB 或 XMLBeans 转换为 java 对象,然后使用 JDBC 或 JPA 将其保存到数据库中。

Bonus idea (Tristan) : In second solution, you may also chose for which portion of XML you actually need precise data in SQL DB and for the other portion you would store it in a CLOB field.额外的想法(Tristan):在第二个解决方案中,您还可以选择在 SQL DB 中实际需要精确数据的 XML 部分,以及将其存储在 CLOB 字段中的另一部分。

Best way is to stick it in your SQL database. 最好的方法是将其粘贴在SQL数据库中。 You can easily run it through SQLizer, which will convert XML to SQL and give you table definitions and multiple INSERT statements. 您可以通过SQLizer轻松运行它,它将将XML转换为SQL,并为您提供表定义和多个INSERT语句。

This will also let your choose specific portions of the XML file to convert. 这也将使您选择要转换的XML文件的特定部分。

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

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