简体   繁体   English

用Java读取XML文件

[英]Reading XML files in Java

I have a big XML file and several POJO clasess needed to read this XML. 我有一个大的XML文件和几个POJO clasess需要读取这个XML。 When i try read test file with one POJO i use this: 当我尝试用一​​个POJO读取测试文件时我使用这个:

    JAXBContext jaxbContext = JAXBContext.newInstance(Test.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Test ts = (Test)jaxbUnmarshaller.unmarshal(file);
System.out.println(ts.getName());

But when i have 30 POJOs what i gonna do? 但是当我有30个POJO时,我会做什么? Create this 4 lines 30 times? 创建这4行30次? Give me advice. 给我建议。

UPDATE UPDATE

How i understand from this example http://blog.bdoughan.com/2010/08/using-xmlanyelement-to-build-generic.html for using several POGOs i gonna use 我如何理解这个例子http://blog.bdoughan.com/2010/08/using-xmlanyelement-to-build-generic.html使用几个POGO我将使用

JAXBContext.newInstance("message:customer:product:order");

And in this examle autor have 3 clesses but only in two of it he whire @XmlRootElement annotation. 在这个考试中,autor有3个咒语,但只有两个,他在@XmlRootElement注释。 Why? 为什么?

You could create JAXBContext with all 30 POJOs you have. 您可以使用所有30个POJO创建JAXBContext。 Also you could store their names in jaxb.index index file in your package, and create JAXBContext.newInstance("your.package") 您还可以将它们的名称存储在包中的jaxb.in​​dex索引文件中,并创建JAXBContext.newInstance(“your.package”)

Here is some details about jaxb.index from javadoc 以下是javadoc中 jaxb.in​​dex的一些细节

Alternatively than being listed in the context path, programmer annotated JAXB mapped classes can be listed in a jaxb.index resource file, format described below. 除了在上下文路径中列出之外,程序员带注释的JAXB映射类可以在jaxb.in​​dex资源文件中列出,格式如下所述。 Note that a java package can contain both schema-derived classes and user annotated JAXB classes. 请注意,java包可以包含模式派生类和用户注释的JAXB类。 Additionally, the java package may contain JAXB package annotations that must be processed. 此外,java包可能包含必须处理的JAXB包注释。 (see JLS 3rd Edition, Section 7.4.1. "Package Annotations"). (参见JLS第3版,第7.4.1节“包注释”)。

Your classes should be annotated with @XmlRootElement or @XmlType annotations. 您的类应使用@XmlRootElement或@XmlType注释进行注释。

Also you could find all classes annotated as @XmlRootElement using scannotation framework, and create JAXBContext with all JAXB POJOs you have. 您还可以使用scannotation框架找到所有注释为@XmlRootElement的类,并使用您拥有的所有JAXB POJO创建JAXBContext。

Please if you have questions, comment and I will update an answer. 如果您有任何问题,请发表评论,我会更新答案。 Hope it helps. 希望能帮助到你。

Ideally, you would create the JAXBContext only once and cache it for re-use. 理想情况下,您只需创建一次JAXBContext并将其缓存以便重复使用。 Now 现在

But when i have 30 POJOs what i gonna do? 但是当我有30个POJO时,我会做什么? Create this 4 lines 30 times? 创建这4行30次?

If all of your 30 POJOs are in the same package (for eg com.abc) then create the context as JAXBContext.newInstance("com.abc") 如果所有30个POJO都在同一个包中(例如com.abc),则将上下文创建为JAXBContext.newInstance("com.abc")

In this examle autor have 3 clesses but only in two of it he whire @XmlRootElement annotation. 在这个考试中,autor有3个咒语,但只有两个,他就是@XmlRootElement注释。 Why? 为什么?

Only the POJOs that correspond to the global element declarations in the XSD Schema have the @XmlRootElement annotation. 只有与XSD Schema中的全局元素声明对应的POJO才具有@XmlRootElement注释。 This is because a global element declaration is a potential root element in an instance document. 这是因为全局元素声明是实例文档中的潜在根元素。

It would be better if you can post a sample of your XML Schema and XML instance document for us to provide a more specific answer. 如果您可以发布XML Schema和XML实例文档的示例,以便我们提供更具体的答案,那就更好了。

The following should help. 以下应该有所帮助。

ANNOTATIONS 说明一

JAXB model classes do not require any annotations. JAXB模型类不需要任何注释。 There is not annotation that indicates that a class should automatically be processed when creating a JAXBContext . 没有注释表明在创建JAXBContext时应自动处理类。

CREATING A JAXBContext 创建一个JAXBContext

There are two main ways of creating a JAXBContext 创建JAXBContext有两种主要方法

1 - On Classes 1 - 在课堂上

You pass in an array of domain classes. 您传入一个域类数组。 Mappings are then created for these classes. 然后为这些类创建映射。 Mappings are also created for referenced (see WHAT CLASSES ARE BROUGHT IN below). 还会为引用创建映射(请参阅下面的“类别”)。

2 - On Context Paths 2 - 关于上下文路径

My article that you cited ( http://blog.bdoughan.com/2010/08/using-xmlanyelement-to-build-generic.html ) uses a context path. 我引用的文章( http://blog.bdoughan.com/2010/08/using-xmlanyelement-to-build-generic.html )使用了上下文路径。 A context path consists of colon delimited package names. 上下文路径由冒号分隔的包名称组成。 Each package must contain either a jaxb.index file or ObjectFactory class. 每个包必须包含jaxb.index文件或ObjectFactory类。 The jaxb.index file is a carriage return separated list of class names you wish to create the JAXBContext on. jaxb.index文件是一个回车分隔的类名列表,您希望在其上创建JAXBContext Just as with creating a JAXBContext on an array of classes, reference classes are also processed. 就像在类数组上创建JAXBContext ,也会处理引用类。

WHAT CLASSES ARE BROUGHT IN 是什么类型的

Below are some of the key concepts involved on what secondary classes are processed when creating a JAXBContext . 下面是创建JAXBContext时处理哪些辅助类所涉及的一些关键概念。

1 - Referenced Classes 1 - 参考类

If a JAXBContext is created on the Foo class, then Bar will also be processed since it is referenced by Foo . 如果在Foo类上创建了JAXBContext ,那么Bar也将被处理,因为它被Foo引用。

@XmlAccessorType(XmlAccessType.FIELD)
public class Foo {

    private List<Bar> bar;

}

2 - Super Classes 2 - 超级课程

If a class is processed then its super class is also processed. 如果处理了一个类,那么它的超类也会被处理。 You can put the @XmlTransient annotation on a class to prevent it from being processed (see: http://blog.bdoughan.com/2011/06/ignoring-inheritance-with-xmltransient.html ). 您可以将@XmlTransient注释放在类上以防止它被处理(请参阅: http@XmlTransient )。

public class Foo extends Bar {
}

3 - Subclasses 3 - 子类

If a class is processed then its subclasses are not automatically processed. 如果处理了类,则不会自动处理其子类。 You can put the @XmlSeeAlso annotation on a class to specify its subclasses that you wish to process. 您可以在类上放置@XmlSeeAlso注释以指定要处理的子类。

@XmlSeeAlso({Bar.class})
public class Foo {
}

4 - Classes Referenced from JAXB annotations 4 - 从JAXB注释引用的类

If a class is processed, then classes specified on JAXB annotations within that class are also processed 如果处理了类,则还会处理在该类中的JAXB注释上指定的类

public class Foo {
    @XmlElements({
         @XmlElement(name="a", type=A.class),
         @XmlElement(name="b", type=B.class)
    })
    private Object bar;
}

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

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