Better way to parse xml

前端 未结 9 2104
感动是毒
感动是毒 2021-02-06 05:05

I\'ve been parsing XML like this for years, and I have to admit when the number of different element becomes larger I find it a bit boring and exhausting to do, here is what I m

9条回答
  •  情书的邮戳
    2021-02-06 05:39

    I've been using xsteam to serialize my own objects to xml and then load them back as Java objects. If you can represent everythign as POJOs and you properly annotate the POJOs to match the types in your xml file you might find it much easier to use.

    When a String represents an object in XML, you can just write:

    Order theOrder = (Order)xstream.fromXML(xmlString);

    I have always used it to load an object into memory in a single line, but if you need to stream it and process as you go you should be able to use a HierarchicalStreamReader to iterate through the document. This might be very similar to Simple, suggested by @Dave.

提交回复
热议问题