在解析Document对象时获取Stuck?

时间:2011-10-06 15:39:45

标签: java xml jdom

我正在尝试解析我的Document对象,就像是;

<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>

我希望得到<variableBinding>以下<variables>以外的所有内容,因为我是XML解析中的菜鸟,所以我真的不知所措。

我正在尝试编写一个函数;

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");
    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;
}

你能帮助我获得<variableBinding>吗?

谢谢大家

3 个答案:

答案 0 :(得分:4)

您是否考虑过使用XPath?

//variableBinding将选择所有variableBinding元素。

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++) {
  ...
}

答案 1 :(得分:2)

你需要做这样的事情:

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;
}

答案 2 :(得分:0)

听起来像SAX解析器会更好用。这个博客提供了一个使用SAX解析器来解析XML的简单示例。

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

---- ---- EDIT 实际上,这是一个更好的http://cs.au.dk/~amoeller/XML/programming/saxexample.html