开发者

how to parse xml using sax by checking both parent and child tags at a time?

Hi I want to parse an xml doc using sax parser. What i want to do is to check out both outer and inner tags(Eg: am having same tags in other tags also. like . ) so i want to get the data based on outer tag i.e only from tag or tag1 in our case. can u please help me with this? can i check both the parent and child开发者_运维百科 tags using sax in java???? Please help me out.

Thanks..


Write a ContentHandler that utilises a Stack to hold data of nested elements. In the beginning of StartElement do Stack.push and in the end of EndElement do Stack.pop. Use a Stack holding just element names or Stack with your own Element class to hold more data.

You may also write a general purpose abstract ContentHandler that holds the Stack and provides its descendants methods for getting all kinds of information of the current element and its path.


I think one way you can do is, if the outer tags are different then when u get the outer tag can set some variable value, then when it comes to inner tag can check which value that variable is set to and know parent tag. for

Ex: 
<x>
 <y/>
</x>
<z>
 <y/>
</z>

so in startElement() of handler you can check if name==x, set variable value to x( else z). when you get y, check what the variable is set to(x/z).

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜