如何从XML文件中读取数据并将其存储到数据库(MySQL)中?

时间:2021-03-11 00:35:44

I need to get data from an XML file and store it into a MySQL Database. I am thinking of using a SAX Parser for parsing the data but I am not sure of how to store data efficiently into database, I am thinking of few technologies like JDBC and Hibernate but I wanted to ask about what would be the efficient way of doing it?

我需要从XML文件中获取数据并将其存储到MySQL数据库中。我正在考虑使用SAX Parser来解析数据,但我不确定如何有效地将数据存储到数据库中,我想到的是像JDBC和Hibernate这样的技术,但是我想问一下有效的方法是什么它?

Note: Here programming language is Java.

注意:这里的编程语言是Java。

4 个解决方案

#1


1  

You could use Castor witch is an open source data binding framework for moving data from XML to Java programming language objects and from Java to databases.

您可以使用Castor witch是一个开源数据绑定框架,用于将数据从XML移动到Java编程语言对象以及从Java移动到数据库。

I found also article series in IBM developerWorks that describe using Castor suited to your needs.

我在IBM developerWorks中找到了一篇文章系列,描述了使用适合您需求的Castor。

#2


4  

I would suggest using JAXB for reading in the XML to Java objects and JPA for writing them into the database. You can create a single data model using Java classes that have both annotations for XML binding using JAXB and database persistence annotations using JPA.

我建议使用JAXB读取XML到Java对象,使用JPA将它们写入数据库。您可以使用Java类创建单个数据模型,这些Java类既包含使用JAXB的XML绑定注释,也包含使用JPA的数据库持久性注释。

@Entity
@Table(name="Foo")
@XmlRootElement
public class Foo {
    // ...
}

Information on JAXB annotations. Information on JPA.

有关JAXB注释的信息。有关JPA的信息。

#3


0  

It depends on many factors. If your XML is too large ( > 1GB or comparable with your total memory), then you should use SAX and I don't think there would be other solutions. If it's small (say smaller than 100MB), simply load the whole XML into a Document object using JAXP:

这取决于很多因素。如果您的XML太大(> 1GB或与您的总内存相当),那么您应该使用SAX,我认为不会有其他解决方案。如果它很小(例如小于100MB),只需使用JAXP将整个XML加载到Document对象中:

DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder parser = documentBuilderFactory.newDocumentBuilder();
Document doc = parser.parse(source);

You probably have elements or attributes mappied to columns on DB. You can then query elements/attrs using XPath for simplicity and write them to DB. It this is a one-time conversion, I recommend using simple JDBC. Don't think for JPA or Hibernate, as it just increases your development time for a routine data conversion scenario.

您可能在DB上的列上映射了元素或属性。然后,您可以使用XPath查询元素/ attrs以简化并将它们写入DB。这是一次性转换,我建议使用简单的JDBC。不要考虑JPA或Hibernate,因为它只会增加日常数据转换方案的开发时间。

#4


0  

You may store XML into mySQL directly using blob... if you want efficient indexing and high performance, VTD-XML has built-in ability to index/query/update XML document, making it a better alternative than SAX and DOM, here is a link to a related article

您可以使用blob直接将XML存储到mySQL中...如果您想要有效的索引和高性能,VTD-XML具有索引/查询/更新XML文档的内置功能,使其成为比SAX和DOM更好的替代方案,这里是指向相关文章的链接

Index XML documents with VTD-XML

使用VTD-XML索引XML文档

#1


1  

You could use Castor witch is an open source data binding framework for moving data from XML to Java programming language objects and from Java to databases.

您可以使用Castor witch是一个开源数据绑定框架,用于将数据从XML移动到Java编程语言对象以及从Java移动到数据库。

I found also article series in IBM developerWorks that describe using Castor suited to your needs.

我在IBM developerWorks中找到了一篇文章系列,描述了使用适合您需求的Castor。

#2


4  

I would suggest using JAXB for reading in the XML to Java objects and JPA for writing them into the database. You can create a single data model using Java classes that have both annotations for XML binding using JAXB and database persistence annotations using JPA.

我建议使用JAXB读取XML到Java对象,使用JPA将它们写入数据库。您可以使用Java类创建单个数据模型,这些Java类既包含使用JAXB的XML绑定注释,也包含使用JPA的数据库持久性注释。

@Entity
@Table(name="Foo")
@XmlRootElement
public class Foo {
    // ...
}

Information on JAXB annotations. Information on JPA.

有关JAXB注释的信息。有关JPA的信息。

#3


0  

It depends on many factors. If your XML is too large ( > 1GB or comparable with your total memory), then you should use SAX and I don't think there would be other solutions. If it's small (say smaller than 100MB), simply load the whole XML into a Document object using JAXP:

这取决于很多因素。如果您的XML太大(> 1GB或与您的总内存相当),那么您应该使用SAX,我认为不会有其他解决方案。如果它很小(例如小于100MB),只需使用JAXP将整个XML加载到Document对象中:

DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder parser = documentBuilderFactory.newDocumentBuilder();
Document doc = parser.parse(source);

You probably have elements or attributes mappied to columns on DB. You can then query elements/attrs using XPath for simplicity and write them to DB. It this is a one-time conversion, I recommend using simple JDBC. Don't think for JPA or Hibernate, as it just increases your development time for a routine data conversion scenario.

您可能在DB上的列上映射了元素或属性。然后,您可以使用XPath查询元素/ attrs以简化并将它们写入DB。这是一次性转换,我建议使用简单的JDBC。不要考虑JPA或Hibernate,因为它只会增加日常数据转换方案的开发时间。

#4


0  

You may store XML into mySQL directly using blob... if you want efficient indexing and high performance, VTD-XML has built-in ability to index/query/update XML document, making it a better alternative than SAX and DOM, here is a link to a related article

您可以使用blob直接将XML存储到mySQL中...如果您想要有效的索引和高性能,VTD-XML具有索引/查询/更新XML文档的内置功能,使其成为比SAX和DOM更好的替代方案,这里是指向相关文章的链接

Index XML documents with VTD-XML

使用VTD-XML索引XML文档