You are currently viewing Parsing XML from internal node – Simple XML Javax

Parsing XML from internal node – Simple XML Javax

Hello guys!

I have a big XML to parse and using DOM is killing me since tags are repeated in a strange way thru the file. I thought about trying for my first time Simple XML serialization but I don’t seem to find the proper way to start parsing the file from the point I want to. Is it possible to serialize just a portion of the file? Say setting the root on a point in the middle of the file?

For instance, say I have this file:

<Top> <Useless_tag_one> ... </Useless_tag_one> <Inner> <Element_one> One </Element_one> <Element_two> Two </Element_two> ... <Element_n> n </Element_n> </Inner> <Useless_tag_two> ... </Useless_tag_two> ... </Top> 

Now let’s suppose I only need to parse the Inner tag and its content, I don’t need the rest. I wrote the following POJO:

@Root(name="Inner",strict = false) class Inner { @field:Element(name="Element_one") lateinit var elementOne :String ... } 

But it gives me this error:

Exception in thread "main" org.simpleframework.xml.core.ValueRequiredException: Unable to satisfy u/org.simpleframework.xml.Element(name="Element_one", type=void.class, data=false, required=true) on field 'elementOne' public java.lang.String 

Is it possible to set the root to be an inner element like this? How can I work around this?

Thanks a lot to everyone!

submitted by /u/NicolaM1994
[link] [comments]