【问题标题】:How to Handle null XML tags using java XPATH?如何使用 java XPATH 处理空 XML 标记?
【发布时间】:2013-12-27 11:07:27
【问题描述】:

如何使用 java XPATH 处理 null XML 标签?

 <?xml version="1.0" encoding="UTF-8"?>
 <Employees>    
 <Employee emplid="1111" type="admin">    
 **<firstname/>**  
 <lastname>Watson</lastname>         
 <age>30</age>         
 <email>johnwatson@sh.com</email>     
 </Employee>     
 <Employee emplid="2222" type="admin">         
 <firstname>Sherlock</firstname>         
 <lastname>Homes</lastname>         
 <age>32</age>         
 <email>sherlock@sh.com</email>     
 </Employee>
 </Employees> 

在上面的XML&lt;firstname/&gt;标签是空的,我怎样才能在不抛出异常的情况下显示默认值?

目前使用:

System.out.println("*************************"); 

expression = "/Employees/Employee/firstname"; 

System.out.println(expression); 

NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(xmlDocument, 
XPathConstants.NODESET);

for (int i = 0; i < nodeList.getLength(); i++) {                 

    System.out.println(nodeList.item(i).getFirstChild().getNodeValue());              

}

【问题讨论】:

    标签: java xml dom xpath sax


    【解决方案1】:

    没有“空标签”之类的东西。我认为您的意思是“空元素”。如果您学会使用正确的行话,您会发现这些问题的答案更容易找到。

    最简单的解决方案是使用返回节点的字符串值的 XPath 表达式,而不是节点本身。尽量遵循XPath尽量做,Java尽量少做的原则,因为XPath是为处理XML而定制的,而Java不是。

    (理想情况下,不要用 Java 处理数据:全部使用基于 XML 的语言,例如 XPath、XSLT 和 XQuery)。

    您可以使用表达式string(/Employees/Employee[1]/firstname)获取第一个员工的字符串值

    【讨论】:

      【解决方案2】:

      运行此 Java 程序,演示如何使用 XPath 获取可能为空的元素内容:

      import java.io.StringReader;
      import javax.xml.xpath.XPath;
      import javax.xml.xpath.XPathFactory;
      import org.xml.sax.InputSource;
      import java.util.Arrays;
      import java.util.List;
      
      public class Try {
          public static void main(String[] args) throws Exception {
              String xml =
                "<?xml version='1.0' encoding='UTF-8'?>"
                + "<Employees>"
                + "  <Employee emplid='1111' type='admin'>"
                + "    <firstname/>"
                + "    <lastname>Watson</lastname>"
                + "    <age>30</age>"
                + "    <email>johnwatson@sh.com</email>"
                + "  </Employee>"
                + "  <Employee emplid='2222' type='admin'>"
                + "    <firstname>Sherlock</firstname>"
                + "    <lastname>Homes</lastname>"
                + "    <age>32</age>"
                + "    <email>sherlock@sh.com</email>"
                + "  </Employee>"
                + "</Employees>";
              List<String> ids = Arrays.asList("1111", "2222");
              for(int i = 0; i < ids.size(); i++) {
                String employeeId = ids.get(i);
                String xpath = "/Employees/Employee[@emplid='" + employeeId + "']/firstname";
                XPath xPath = XPathFactory.newInstance().newXPath();
                String employeeFirstName = xPath.evaluate(xpath, new InputSource(new StringReader(xml)));
                if (employeeFirstName == "") {
                  System.out.println("Employee " + employeeId +  " has no first name.");
                } else {
                  System.out.println("Employee " + employeeId + "'s first name is " + employeeFirstName);
                }
              }
          }
      }
      

      会产生这个输出:

      Employee 1111 has no first name.
      Employee 2222's first name is Sherlock
      

      根据 OP 在 cmets 中的请求进行更新

      运行这个 Java 程序纠正 OP 的 NodeList 处理:

      import java.io.StringReader;
      import javax.xml.xpath.XPath;
      import javax.xml.xpath.XPathConstants;
      import javax.xml.xpath.XPathFactory;
      import org.xml.sax.InputSource;
      import org.w3c.dom.NodeList;
      import org.w3c.dom.Node;
      
      public class Try {
          public static void main(String[] args) throws Exception {
              String xml =
                "<?xml version='1.0' encoding='UTF-8'?>"
                + "<Employees>"
                + "  <Employee emplid='1111' type='admin'>"
                + "    <firstname/>"
                + "    <lastname>Watson</lastname>"
                + "    <age>30</age>"
                + "    <email>johnwatson@sh.com</email>"
                + "  </Employee>"
                + "  <Employee emplid='2222' type='admin'>"
                + "    <firstname>Sherlock</firstname>"
                + "    <lastname>Homes</lastname>"
                + "    <age>32</age>"
                + "    <email>sherlock@sh.com</email>"
                + "  </Employee>"
                + "</Employees>";
              System.out.println("*************************");
              String expression = "/Employees/Employee/firstname";
              System.out.println(expression);
              XPath xPath = XPathFactory.newInstance().newXPath();
              NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(new InputSource(new StringReader(xml)),
                                                                                XPathConstants.NODESET);
              for (int i = 0; i < nodeList.getLength(); i++) {
                if (nodeList.item(i).getFirstChild() == null)
                  System.out.println("Employee has no first name.");
                else
                  System.out.println(nodeList.item(i).getFirstChild().getNodeValue());
              }
          }
      }
      

      会产生这个输出:

      /Employees/Employee/firstname
      Employee has no first name.
      Sherlock
      

      【讨论】:

      • 我添加了包含您的代码 sn-p 的工作代码。作为回报,我请您accept some answer 回答这个问题,并回答您之前帮助您的问题。你过去得到了很好的答案;您应该通过投票accepting适当的答案来感谢那些花时间提供帮助的人。谢谢。
      猜你喜欢
      • 2018-06-11
      相关资源
      最近更新 更多