Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
574 views
in Technique[技术] by (71.8m points)

java - JAXB - Ignore element

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: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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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;
}

then simply removing the bar field from Foo will skip the <bar/> element in the input document.

Alternatively, annotated the field with @XmlTransient instead of @XmlElement, and it will also be skipped.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...