开发者

How to parse XML by SAX and Create Tree list?

I have learn开发者_C百科ed a tutorial about SAX parser of java and now I need to create tree list of XML file like this:

<course>
     <chapter>
          <title>chapter one of course one</title>
          <content> .... </content>
     </chapter>

    <chapter>
          <title>chapter two of course one</title>
          <content> .... </content>
    </chapter>
</course>

and then I what to create data structure like this:

string lessons[][] = {
       {"chapter one of course one", "chapter two of course one"},
       {"chapter one of course two", "chapter two of course two", "chapter tree of course two"},
       ....
       ....

}


There is no out-of-box support in JDK for the kind of data structure you are looking for. What you need is a Multimap. Guava library provides this data structure. You can either use that library (I recommend) or build your own.

Also, make sure you read my answer in this SO post if your are reading element content using SAX.

JAVA SAX parser split calls to characters()


here you go: THIS is SAX documentation, read and try to build your own parser mate


Think in terms of a a Stack. In startElement() you push and in endElement you pop(). Depending on the element tag name you do interesting stuff with what's on the stack and perhaps also what you have in a "global" context.


This code exactly does what you're looking for, You just need to tweak it to output the JSON structure you want.

https://github.com/kasmodia/sax-tree-parser

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜