簡體   English   中英

為什么xPath查詢沒有返回正確的輸出?

[英]Why does xPath query does not return correct output?

我的XML文件的布局為:

<?xml version="1.0" encoding="UTF-8"?>
<PasswordVault>
  <User id="1">
    <Log LOG="1">
      <AccountType>asd</AccountType>
      <Username>asd</Username>
      <Password>asd</Password>
      <E-mail>asd</E-mail>
    </Log>
    <Log Log="3">
      <AccountType>as</AccountType>
      <Username>as</Username>
      <Password>as</Password>
      <E-mail>as</E-mail>
    </Log>
  </User>
</PasswordVault>

而且我已經編寫了Java代碼:

public class GetMaxLog {
        public static void main(String[] args) throws Exception {

        String y = x();
        System.out.println(y);

    }
    public static String x() throws Exception{  
        try {
            DocumentBuilderFactory factory = 
               DocumentBuilderFactory.newInstance();
            factory.setNamespaceAware(true); 
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document doc = (Document)builder.parse("FILE PATH");

            XPathFactory xpathfactory = XPathFactory.newInstance();
            XPath xpath = xpathfactory.newXPath();

            XPathExpression expr = xpath.compile("//PasswordVault/User[@id = 1]/Log[not(@LOG < ../Log/@LOG)]/@LOG");

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

          //returns the element at tag value 0 thus only the first set of data    
           return  nodes.item(0).getNodeValue();
        }catch(SAXParseException e){
            return "0";
        }
    }
 }

當最大日志值明顯為3時,該函數始終返回1。我如何修復此函數以始終輸出最大日志值。

XML名稱區分大小寫。 @LOG的最大值為1。要解決您遇到的問題,請從

<Log Log="3">

<Log LOG="3">

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM