JAXB - 忽略元素 [英] JAXB - Ignore element
问题描述
有没有办法忽略Jaxb解析中的元素?
我有一个大的XML文件,如果我可以忽略其中一个大的复杂元素,那么它可能会更快地解析。
Is there any way to just ignore an element from Jaxb parsing? I have a large XML file, and if I could ignore one of the large, complex elements, then it would probably parse a lot quicker.
它会如果甚至根本无法验证元素内容并解析文档的其余部分,即使该元素不正确,也会更好。
It would be even better if it could not even validate the element contents at all and parse the rest of the document even if that element is not correct.
ex:这应该只生成Foo.element1和Foo.element2
ex:this should only generate Foo.element1 and Foo.element2
<foo>
<element1>I want this</element1>
<element2>And this</element2>
<bar>
<a>ALL of bar should be ignored</a>
<b>this also should be ignored</b>
<c>
<x>a lot of C that take time to process</x>
</c>
<c>
<x>a lot of C that take time to process</x>
</c>
<c>
<x>a lot of C that take time to process</x>
</c>
<c>
<x>a lot of C that take time to process</x>
</c>
</bar>
</foo>
推荐答案
假设您的JAXB模型如下所示:
Assuming your JAXB model looks like this:
@XmlRootElement(name="foo")
public class Foo {
@XmlElement(name="element1")
String element1;
@XmlElement(name="element2")
String element2;
@XmlElement(name="bar")
Bar bar;
}
然后只需删除栏
Foo
中的字段将跳过输入文档中的< bar />
元素。
then simply removing the bar
field from Foo
will skip the <bar/>
element in the input document.
或者,使用 @XmlTransient
而不是 @XmlElement
注释该字段,它也会被跳过。
Alternatively, annotated the field with @XmlTransient
instead of @XmlElement
, and it will also be skipped.
这篇关于JAXB - 忽略元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!