1

I have the following XML:

<?xml version="1.0" encoding="utf-16"?>
<ROOT>
    <fieldset>
        <field Id="c0" Name="_ID_VERSION" DataType="int"/>
        <field Id="c1" Name="_TYPE" DataType="string"/>
        <field Id="c2" Name="_PRODUCT" DataType="string"/>
        <field Id="c3" Name="_VERSION" DataType="string"/>
        <field Id="c4" Name="_REVISION" DataType="int"/>
        <field Id="c5" Name="_STATE" DataType="string"/>
        <field Id="c6" Name="_DOCUMENT" DataType="int"/>
        <field Id="c7" Name="_HAS_LINK" DataType="int"/>
        <field Id="c8" Name="_LOCKED" DataType="int"/>
        <field Id="c9" Name="_ACCESSLEVEL" DataType="int"/>
        <field Id="c10" Name="_LABEL" DataType="int"/>
        <field Id="c11" Name="_LABEL_NAME" DataType="string"/>
    </fieldset>
    <rowset>
        <row c0="2932" c1="" c2="{3FDAC10D-5361-4C32-B79E-D391DCB3AE98}" c3="" c4="0" c5="" c6="0" c7="1" c8="0" c9="3" c10="-1" c11=""/>
        <row c0="3146" c1="0" c2="MyProject" c3="" c4="0" c5="" c6="0" c7="1" c8="1" c9="3" c10="-1" c11=""/>
        <row c0="3147" c1="" c2="tes" c3="" c4="0" c5="" c6="0" c7="1" c8="0" c9="3" c10="-1" c11=""/>
        <row c0="2933" c1="" c2="" c3="" c4="0" c5="" c6="0" c7="1" c8="0" c9="3" c10="-1" c11=""/>
        <row c0="3048" c1="" c2="" c3="" c4="0" c5="" c6="0" c7="1" c8="0" c9="3" c10="-1" c11=""/>
    </rowset>
</ROOT>

How to I can map this text to POJO in java? I have tried parsing it with the following code:

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
Document document = null;
try {
    DocumentBuilder db = dbf.newDocumentBuilder();
    InputSource is = new InputSource(new StringReader(incomming));
    document = db.parse(is);
} catch (ParserConfigurationException | SAXException | IOException e) {
    e.printStackTrace();
}
return document;

But I want to do it automatically.

5
  • This: <row c0="3147" c1="0?:0 :>=B@035=B>2" c2="tes" c3="" c4="0" c5="!CI5AB2C5B" c6="0" c7="1" c8="0" c9="3" c10="-1" c11=""/> does not look like valid XML. Commented Oct 13, 2014 at 15:10
  • Sorry xml prettify error encode it Commented Oct 13, 2014 at 15:51
  • What should your pojo look like? Commented Oct 13, 2014 at 17:36
  • Do you have a schema? Do you want to get something with properties idVersion, type, product etc. or fieldset, rowset etc? Commented Oct 13, 2014 at 17:45
  • not i don't schema file. Yes it API out from programm. I want convert it to pogo for work with it in next steps. Commented Oct 13, 2014 at 18:09

1 Answer 1

3

This question probably hits most of the SO offtopic list. :) Recommend-a-tool, opinion-based, what-have-you-tried-so-far-etc. But, hey, whatever. :)

Here's an opinion-based tool recommendation of what I like, namely JAXB.

Write and annotate your POJOS:

@XmlRootElement(name="ROOT")
public class Dataset {

  @XmlElementWrapper(name="fieldset")
  @XmlElement(name="field")
  public List<Field> fields = new LinkedList<Field>();

  @XmlElementWrapper(name="rowset")
  @XmlElement(name="row")
  public List<Row> rows = new LinkedList<Row>();

}

public class Field {
  @XmlElement(name="Id")
  public String id;
  @XmlElement(name="Name")
  public String name;
  @XmlElement(name="DataType")
  public String dataType;
}

public class Row {
  @XmlAnyElement
  public Map<QName, String> columns = new HashMap<QName, String>();
}

Create a JAXBContext for your POJOs:

final JAXBContext context = JAXBContext.newInstance(Dataset.class);

"Parsing" in JAXB is called "unmarshalling:

final Unmarshaller = contect.createUnmarshaller();
final Dataset dataset = (Dataset) unmarshaller.unmarshal(source);

I just sketched the code right here, so this won't probably work from the very start, but you get the idea.

I am also not sure that these are the POJOs you want, but it might be good to start with.

Apart from JAXB, there's probably a few dozens other libraries you could use for the task. Please don't take JAXB as the only answer, check alternatives as well.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.