开发者

Get Stuck while parsing Document object?

I am trying to parse my Document object which is like;

<message>
<header>
<messageType>snmp</messageType>
<sendFrom>localhost</sendFrom>
<hostName>localhost</hostName>
<sendTo>192.168.0.15</sendTo>
<receiverName>receiverHost</receiverName>
<date>06/10/2011 17:34:55</date>
</header>
<body>
<snmpType>set</snmpType>
<ip>127.0.0.1</ip>
<community>public</community>
<variables>
  <variableBinding>
    <variable>TechDive.in</variable>
    <OID>1.3.6.1.4.1.8072.3.2.10</OID>
  </variableBinding>
  <variableBinding>
    <variable>TechDive.in</variable>
    <OID>1.3.6.1.4.1.8072.3.2.10</OID>
  </variableBinding>
  <variableBinding>
    <variable>TechDive.in</variable>
    <OID>1.3.6.1.4.1.8072.3.2.10</OID>
  </variableBinding>
  <variableBinding>
    <variable>TechDive.in</variable>
    <OID>1.3.6.1.4.1.8072.3.2.10</OID>
  </variableBinding>
  </variables>
  </body>
 </message>

I want to get all off the <variableBinding>s which are under the <variables>, I really get stuck about it since I am a noob in XML parsing.

I am trying to write a function which is ;

public Vector<VariableBinding> getVariables(Document document){ 

    Vector<VariableBinding> variables = new Vector<VariableBinding>();

    Element root = document.getRootElement();
    Element body = root.getChi开发者_如何转开发ld("body");
    Element oidsElement = body.getChild("variables");
    Element variable = oidsElement.getChild("variable");

    for (Object currentOidObj : variable.getChildren()) {

        Element currentOid = (Element) currentOidObj;
        String oid = currentOid.getText();
        Element currentVariable = (Element) currentOidObj;

        Variable var = new OctetString(currentVariable.getText());
        OID oidObj = new OID(oid);

        VariableBinding v = new VariableBinding(oidObj,var);
        variables.add(v);
    }
    return variables;
}

Can you please help me to get <variableBinding>s ?

Thank you all


Have you considered using XPath?

//variableBinding will select all variableBinding elements.

E.g.:

XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
XPathExpression expr = xpath.compile("//variableBinding");

Object result = expr.evaluate(doc, XPathConstants.NODESET);

NodeList nodes = (NodeList) result;
for (int i = 0; i < nodes.getLength(); i++) {
  ...
}


You need to do something likes this:

public Vector<VariableBinding> getVariables(Document document){ 

Vector<VariableBinding> variables = new Vector<VariableBinding>();

Element root = document.getRootElement();
Element body = root.getChild("body");
Element oidsElement = body.getChild("variables");

for (Object ovb: oidsElement.getChildren("variableBinding")) {

    Element vb = (Element) ovb;
    Variable var = new OctetString(vb.getChild("OID").getText());
    OID oidObj = new OID(vb.getChild("variable").getText());

    VariableBinding v = new VariableBinding(oidObj,var);
    variables.add(v);
}
return variables;
}


Sounds like a SAX parser would be better for you to use. This blog gives a simple example of using a SAX parser to parse XML.

http://www.mkyong.com/java/how-to-read-xml-file-in-java-sax-parser/

----EDIT---- Actually, here's a better one http://cs.au.dk/~amoeller/XML/programming/saxexample.html

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜