Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ignore unused XML elements while deserializing a document?

I'm using SimpleXml to (de)serialize POJOs. Now, I have a big XML which has some elements which are not needed. For instance, with this XML:

<Root>    <Element>Used</Element>    <Another>Not used</Another> <Root>  

I want to create a POJO which looks like:

@Root class Root{     @Element     private String element; } 

Problem is that I'm getting this Exception:

simpleframework.xml.core.ElementException: Element 'Another' does not have a match in class blah.blah.Blah at line 1 

So... how should I configure the POJO so that I can parse the XML correctly?

like image 719
Cristian Avatar asked Jan 19 '11 21:01

Cristian


People also ask

How to ignore a field in XML response?

Ignore XML Attribute. You can specify ignore="true" or ignore="false". The default value is false. Specifying ignore="false" has no effect on the attribute value assigned when an object of the type specified in the rule is created and no effect on the constraints.

What is @xmlelement?

XML elements can be defined as building blocks of an XML. Elements can behave as containers to hold text, elements, attributes, media objects or all of these. Each XML document contains one or more elements, the scope of which are either delimited by start and end tags, or for empty elements, by an empty-element tag.


1 Answers

Set strict to false within the Root annotation to ignore any XML elements or attributes that do not appear in the class.

@Root(strict=false) 

Alternatively, set strict to false when you read the xml in the serialiser:

Root root = serializer.read(Root.class, source, false); 
like image 80
dogbane Avatar answered Sep 22 '22 07:09

dogbane