简体   繁体   中英

Create JAXB Object from large XML file

I'm looking for something to easily create an Object to access a large XML file.

The XML file looks like this:

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

Like:

List vzg_id=vzg.auswahl.bst;

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.

What I've done now:

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

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

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. Simple Description: XML File

<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; int id_one=subsubchilds.get(0);

Thanks in advance

It is rarely a good idea to write JAXB classes by hand for existing XML. JDK has special command line tool to generate these classes for you (xjc) from XML schema. 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.

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