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.
例如:这应该只生成 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屋!