0

我将使用 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.的值提前谢谢

我曾尝试使用此代码进行解析。

      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();
}
4

1 回答 1

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

于 2012-10-03T13:41:24.390 回答