如何在java中使用DOM解析器解析xml标记?

时间:2012-10-03 13:05:20

标签: java xml dom xml-parsing

我将使用DOM解析器解析java中的xml文件,但是没有获取从我的xml文件中检索某些标记的信息,所以请帮我解决这个问题。

<policy name="DayCapturePolicy" type="capture">
<starttime>9:00:00 AM</starttime>
<endtime>9:00:00 PM</endtime>
<event name="NEW_APP_INSTALLED">
 <eventFilter selectedAppName="Alarm"/> 
   <eventParam>selectedAppName</eventParam>
  <eventParam>selectedAppVersion</eventParam>
  <eventParam>appPackageName</eventParam>
  <eventParam>appDirPath</eventParam>
</event>
</policy>

我想知道如何从上面的xml文件中解析事件过滤器标记的值?我还想知道如何从eventParam标记中检索所有值。我能够检索第一个标记ieselectedAppName的值但是无法解析salectedAppVersion.appPackageName的值,appDirPath.Thanks提前

我曾尝试过这段代码进行解析。

      NodeList nodeList = document.getElementsByTagName("policy");
    Log.d("Reminder", "Number of Policies : "+nodeList.getLength());
    for(int i=0;i<nodeList.getLength();i++){
        Log.d("Reminder", "In 1");
        //PolicyInfo policyInfo = new PolicyInfo();
        Node node = nodeList.item(i);
        NamedNodeMap namedNodeMap = node.getAttributes();
        String policyName = namedNodeMap.getNamedItem("name").getNodeValue();
        String policyType = namedNodeMap.getNamedItem("type").getNodeValue();
        if (node.getNodeType() == Node.ELEMENT_NODE) {

              Element eElement = (Element) node;
              Log.d("startTime  : " ,""+ getTagValue("starttime", eElement));
              Log.d("endtime  : " ,""+ getTagValue("endtime", eElement));
              NodeList nList = document.getElementsByTagName("event");
              for(int j=0;i<nList.getLength();i++){
                    Log.d("Reminder", "In 1");
                    //PolicyInfo policyInfo = new PolicyInfo();
                    Node innerNode = nList.item(i);
                    NamedNodeMap innerNamedNodeMap = innerNode.getAttributes();
                    String eventName = innerNamedNodeMap.getNamedItem("name").getNodeValue();
                    Log.d("innerNode", eventName);
                    if(innerNode.getNodeType()== Node.ELEMENT_NODE){
                         Element innerElement = (Element) innerNode;
                         Log.d("eventParam  : " ,""+ getTagValue("eventParam", innerElement));
                         Log.d("eventFilter  : " ,""+ getTagValue("eventFilter", innerElement));
                         //Log.d("endtime  : " ,""+ getTagValue("endtime", eElement));
                    }
              //System.out.println("starttime : " + getTagValue("starttime", eElement));
             // System.out.println("endtime: " + getTagValue("endtime", eElement));
              //System.out.println("Nick Name : " + getTagValue("nickname", eElement));
              //System.out.println("Salary : " + getTagValue("salary", eElement));

           }
        }


private static String getTagValue(String sTag, Element eElement) {
    NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes();

    Node nValue = (Node) nlList.item(0);

    return nValue.getNodeValue();
}

1 个答案:

答案 0 :(得分:0)

我建议您使用XPath表达式,这样更容易。由于您已经拥有Document,只需创建一个XPath:

XPathFactory xPathFactory = XPathFactory.newInstance();
XPath xPath = xPathFactory.newXPath();
XPathExpression expr = xPath.compile("/policy/event/eventFilter/@selectedAppName");

NodeList nl = (NodeList) expr.evaluate(doc , XPathConstants.NODESET);

for (int i = 0; i < nl.getLength(); i++) {
    Node node = nl.item(i);
    System.out.println(node.getTextContent());
}

那将打印

Alarm

要使所有<eventParam>将xpath表达式切换为/policy/event/eventParam

那将打印

selectedAppName
selectedAppVersion
appPackageName
appDirPath

链接到oracle xpath javadoc:http://docs.oracle.com/javase/7/docs/api/javax/xml/xpath/package-summary.html