简体   繁体   English

从大型XML文件创建JAXB对象

[英]Create JAXB Object from large XML file

I'm looking for something to easily create an Object to access a large XML file. 我正在寻找一些可以轻松创建Object来访问大型XML文件的东西。

The XML file looks like this: XML文件如下所示:

<?xml version="1.0" encoding="WINDOWS-1252"?>
    <vzg:vzg erstellt_von="##" erstellt_am="###" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:vzg="###" xsi:schemaLocation="###">
      <auswahl sicht="B" basisfplp_id="0" basisve_id="0">
        <fplp vzg_id="0" periode="2012/2013"/>
        <version vzg_id="###" name="###"/>
        <strecke name="11801">
          <von baukms_nr="###" km="#.#"/>
          <bis baukms_nr="###" km="#.#"/>
        </strecke>
        <bst vzg_id="#" name="#" kurzbez="##" bez="####" kritart="#"/>
        <bst vzg_id="#" name="#" kurzbez="##" bez="####" kritart="#"/>
        <bst vzg_id="#" name="#" kurzbez="##" bez="####" kritart="#"/>
        <bst vzg_id="#" name="#" kurzbez="##" bez="####" kritart="#"/>
        <bst vzg_id="#" name="#" kurzbez="##" bez="####" kritart="#"/>
        ...

I want an Object to calculate with some of the XML Attributes. 我想要一个Object用一些XML属性来计算。

Like: 喜欢:

List vzg_id=vzg.auswahl.bst; 列出vzg_id = vzg.auswahl.bst;

int res=vzg_id.get(3) * vzg.auswahl.strecke.von.baukms_nr; int res = vzg_id.get(3)* vzg.auswahl.strecke.von.baukms_nr;

Since the XML has about 16000 Lines it's difficult to create a class for every XMLElement. 由于XML有大约16000行,因此很难为每个XMLElement创建一个类。

What I've done now: 我现在做了什么:

MainClass MainClass

import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

import java.io.File;

import javax.xml.bind.JAXB;

public class Main
{
    public static void main(String[] args)
    {
        VZG vzg = JAXB.unmarshal(new File("./XMLVZG.xml"), VZG.class);


        System.out.println(vzg.erstellt_am+ " "+vzg.erstellt_von+"\n"+vzg.aw.sicht);
    }
}

Class VZG VZG级

import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class VZG
{
    @XmlElement(name="auswahl")
    AuswahlSicht aw;
    @XmlAttribute(name="erstellt_von")
    String erstellt_von;
    @XmlAttribute(name="erstellt_am")
    String erstellt_am;
    @XmlAttribute(name="xsi")
    String xmlns_xsi;   
}

Class Auswahl Auswahl班

import java.util.List;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="auswahl")
public class AuswahlSicht
{
    @XmlAttribute(name="basisfplp_id")
    int basisfplp_id;
    @XmlAttribute(name="basisve_id")
    int basisve_id;
    @XmlAttribute(name="sicht")
    String sicht;

}

So I'm now able to get the Attributes of the Root and the Cild, but I have still about 1000 childs with Attributes left and I'm looking for an automated way to parse the XML to get an object. 所以我现在能够获得Root和Cild的属性,但是我仍然有大约1000个具有Attributes的子节点,我正在寻找一种自动解析XML来获取对象的方法。 Simple Description: XML File 简单描述:XML文件

<root>
 <child>
   <Subchild id="1"/>
    <subsubchild id=2/>
    <subsubchild id=33/>
 </child>
</root>

The object should then be like this: 该对象应该是这样的:

List subsubchilds = root.child.subchild.subsubchild; List subsubchilds = root.child.subchild.subsubchild; int id_one=subsubchilds.get(0); int id_one = subsubchilds.get(0);

Thanks in advance 提前致谢

It is rarely a good idea to write JAXB classes by hand for existing XML. 对于现有的XML,手动编写JAXB类很少是个好主意。 JDK has special command line tool to generate these classes for you (xjc) from XML schema. JDK具有特殊的命令行工具,可以从XML模式为您(xjc)生成这些类。 If schema is not available you could try to generate schema from XML (various tools can do that - for example XMLSpy) and then generate classes using xjc. 如果架构不可用,您可以尝试从XML生成架构(各种工具可以这样做 - 例如XMLSpy),然后使用xjc生成类。

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

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