简体   繁体   中英

How to detect xml arrays in java using stax?

I'm trying to parse an XML file using STAX. I'm using the events:

-XMLStreamConstants.START_ELEMENT
-XMLStreamConstants.CHARACTERS
-XMLStreamConstants.END_ELEMENT

Is there any event or a "tricky way" to detect the existence of an array in XML? The main problem is that I don't know the format of the XML being parsed, it can be any XML of any format.

Note that I don't want to use other libraries because the XML file is huge and I don't want to load all the file to the memory.

In json reader we have the events:

-BEGIN_ARRAY
-END_ARRAY

So I'm searching for a way like this to detect the begining of an array in XML using STAX.

Thank you

A Stack<String> of the nested elements, like book, chapter, paragraph, sentence. On start and end of element push/pop onto/from the stack, remember the previous element, and put repeated elements in an "array", a List<Object> , json [...] , where Object might be Map<String, Object> , json { field: ..., ... } .

<book>
    <author />
    <chapter>
         <paragraph>
             <sentence>
             <sentence>


Map
    "book" : Map
          "author" : String
          "chapter" : List
               Map
                   "paragraph" : List
                       Map
                           "sentence" : List
                               String
                               String

Stack<String> tagNesting = new Stack<>();
String priorTag = "";
Map<String, Object> parentValue;

Normally every element is a Map ( LinkedHashMap would conserve order) with as entries XML attributes and XML child elements. Place the attributes with key "@" + name , and the elements just with key name .

If an element is repeated, check the prior entry (Map, or already List) and ad to it, so there is one List.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM