简体   繁体   English

如何使用stax检测Java中的xml数组?

[英]How to detect xml arrays in java using stax?

I'm trying to parse an XML file using STAX. 我正在尝试使用STAX解析XML文件。 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? 是否有任何事件或“棘手的方式”来检测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. 主要问题是我不知道要解析的XML的格式,它可以是任何格式的任何XML。

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. 请注意,我不想使用其他库,因为XML文件很大,并且我不想将所有文件都加载到内存中。

In json reader we have the events: 在json阅读器中,我们有以下事件:

-BEGIN_ARRAY
-END_ARRAY

So I'm searching for a way like this to detect the begining of an array in XML using STAX. 所以我正在寻找一种使用STAX来检测XML数组开头的方法。

Thank you 谢谢

A Stack<String> of the nested elements, like book, chapter, paragraph, sentence. 嵌套元素(如书,章,段落,句子)的Stack<String> 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: ..., ... } . 在元素从栈上push/pop开始和结束时,记住上一个元素,并将重复的元素放在“数组”中,即List<Object> ,json [...] ,其中Object可能是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. 通常,每个元素都是一个Map( LinkedHashMap将保留顺序),并带有XML属性和XML子元素作为条目。 Place the attributes with key "@" + name , and the elements just with key name . 使用键"@" + name放置属性,并使用键name放置元素。

If an element is repeated, check the prior entry (Map, or already List) and ad to it, so there is one List. 如果重复一个元素,请检查先前的条目(“ Map”或“ List”或“ List”)并对其进行广告,因此只有一个“ List”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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