开发者

How to parse the XML file of the given format?

开发者 https://www.devze.com 2022-12-12 05:29 出处:网络
I\'ll be happy if I canhave a sample code to parse the XML of the following format using DOM/SAX parser..

I'll be happy if I can have a sample code to parse the XML of the following format using DOM/SAX parser..

<MMV>Sysdecs000110010101</MMV>
<Protocol name="SNMP">

    <CS CommandString="wmanIf2BsOfdmaTTG" oid="1.3.6.1.2.1.10.184.1.1.6.2.2.1.3.1" Get_SecurityString="public" Set_SecurityString="private" type="integer"></CS> 
    <CS CommandString="wmanIf2BsOfdmaRTG" oid="1.3.6.1.2.1.10.184.1.1.6.2.2.1.4.1" Get_SecurityString="public" Set_SecurityString="private" type="integer"></CS>
    <CS CommandString="wmanIf2BsOfdmaFrameDurationCode" oid="1.3.6.1.2.1.10.184.1.1.6.2.2.1.9.1" Get_SecurityString="public" Set_SecurityString="private" type="integer"></CS>
</Protocol>
<Protocol name="CLI">
    <CS CommandSt开发者_如何学Pythonring="show clock" mode="usermode" type="get" username="aaa" password="bbb"/>
    <CS CommandString="show version" mode="usermode" type="get" username="bbb" password="ccc"/>
    <CS CommandString="set username" mode="configrmode" type="set" username="cc" password="ddd"/>
</Protocol>

Thank u.........


Go through this simple tutorial to understand DOM and SAX parsing and try to write code on your own. If you face difficulty then ask specific question.


Writing a SAX ContentHandler would BARELY be hard to write in this.

The overloaded startElement function can just check if you're in a Protocol or a CS, and depending on which one, append characters from the overridden characters function, and depending on which state you're in, append the characters to one of a handful of strings.

http://www.devshed.com/c/a/Python/Parsing-XML-with-SAX-and-Python/2/

This tutorial above is for Python, but since Python's pretty easy to read, you should be able to get the gist of what needs to happen to parse this.


I'd suggest using jDom. It's quite a while ago that I had to parse XML files in Java, but when I had to do so I always used jDom in combination with XPath. This makes it very easy to navigate through the list of elements and having appropriate methods which given an XML element return you - for instance - a Protocol object.

public List<Protocol> parseXMLDoc(){
   List<Protocol> protocolObjs = new ArrayList<Protocol>();

   ...

   Document doc = ....; //the xml DOM document
   Element root = doc.getRootElement();

   List<Element> protChildElements = root.getChildren();
   foreach(Element protocolElement : protChildElements){
      Protocol obj = getProtocolObj(protocolElement);
      if(obj != null)
         protocolObjs.add(obj);

   }

   return protocolObjs;
}


private Protocol getProtocolObj(Element xmlProtocolElement){
   Protocol result = new Protocol();

   //parse the xml elements and set the data
   //through according setters of the Protocol obj
   Element csEl = xmlProtocolElement.getChild("cs");
   CS csObj = getCSObj(csEl);
   result.setCS(csObj);

   ...

   return result;
}

Hope you got my idea. Note, I wrote this out of my head, so I cannot guarantee that it will work :)


Problem with your XML snippet is that it is not well-formed. It looks more like a multiple XML documents. Standard libraries will have problem with this. It might be possible to use SAX parser or pull parser, with resetting parser after it reports end of root element. After you use your splitter, you should be able to feed these SAX events into libraries like XOM. This is just an idea, I've never tried it though.

0

精彩评论

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