简体   繁体   English

java xml解析大文件和db关系

[英]java xml parsing a large file and db relation

I have a 10+ MB xml file with consists of node(about 10K to 20K) with relations. 我有一个10+ MB的xml文件,包含有关系的节点(大约10K到20K)。

<.....>
<Emplyoyee>
    <name>Jack</name>
    <age>35</age>
    <supervisor></supervisor>
    <....>
</Emplyoyee>
<.....>
<.....>
<.....>
<Emplyoyee>
    <name>Smith</name>
    <age>20</age>
    <supervisor>Jack</supervisor>
    <....>
</Emplyoyee>
<.....>

Now, I want to parse this file and store all the details in DB with "Employee" table which has a field(ID) called "supervisorID". 现在,我想解析这个文件并将所有细节存储在DB中,其中“Employee”表有一个名为“supervisorID”的字段(ID)。 Until now I have tried to make a List of all the employees and then iterating the List for finding supervisor relation. 到目前为止,我已尝试制作所有员工的列表,然后迭代列表以查找主管关系。

Please suggest me a memory efficient and faster way to do this. 请建议我一个有效的内存和更快的方法来做到这一点。 What libraries can I use to handle this type of problems. 我可以使用哪些库来处理此类问题。

You can convert data from XML file to Java Objects using JAXB and insert Java objects to database using Hibernate + JPA . 您可以使用JAXB将数据从XML文件转换为Java对象,并使用Hibernate + JPA将Java对象插入数据库。
You can create 2 DTO 您可以创建2个DTO
Emplyoyee - with all info about Emplyoyee (name, age, ...) Emplyoyee - 关于Emplyoyee的所有信息(姓名,年龄,......)
and
Emplyoyees with List<Emplyoyee> for JAXB unmarshalling EmplyoyeesList<Emplyoyee>为JAXB数据编

EDIT: WITHOUT JAXB and JPA 编辑:没有JAXB和JPA

You can parse file using javascript and send SQL queries usinf Ajax 您可以使用javascript解析文件并使用Ajax发送SQL查询

var xmlDoc = new ActiveXObject("MSXML.DOMDocument");  
xmlDoc.async = false;
xmlDoc.preserveWhiteSpace = true;
xmlDoc.load(pathToFile);
var nodes = xmlDoc.selectNodes("/Emplyoyee");  
for (var node = nodes.nextNode(); node != null; node = nodes.nextNode())
{
   // get another nodes, create SQL query and sent it to server usinj Ajax  
}

Look at MOXy framework provided by EclipseLink . 查看EclipseLink提供的MOXy框架。 It uses JAXB implementation behind the scenes in fact. 它实际上是在幕后使用JAXB实现的。 But does also the ORM stuff with JPA . 但是ORM也与JPA有关

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

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