XPath独立运行,但在放入Web应用程序时不起作用

时间:2014-09-27 11:27:48

标签: java xml xpath expression

我希望解析XML,在xpath表达式的帮助下,我想引入节点名为AuditRecord的所有节点值。令人惊讶的是,我可以在独立应用程序中执行此操作,但相同的代码在应用程序中失败我甚至没有改变XML。我在独立应用程序和Web应用程序中使用相同的XML文件我可以在standlone应用程序中看到结果,但是当我在webapplication中尝试相同的操作时,我得到0个节点列表大小。

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale; 
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;


public class TestXmlNode {
private static final String TEST_XML = "C:/test.xml";
public static void main(String[] args) throws ParseException {
    try {
        //XPath xPath =  XPathFactory.newInstance().newXPath();
        /*System.out.println("*************************");
        String expression = "//AuditRecord/DateTimeStamp";;
        System.out.println("*************************");
        System.out.println(expression);
        NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(xmlDocument,     XPathConstants.NODESET);
        System.out.println("Nodelist size"+nodeList.getLength());
        for (int i = 0; i < nodeList.getLength(); i++) {
            String date = nodeList.item(i).getFirstChild().getNodeValue();
          System.out.println("Date after split     "+date.split("T")[0]);
            Date thedate = convertStringToDate(date);
            Date  todate = convertStringToDate("2014-01-01"); 
            System.out.println("XML todate"+todate.toString()); 
        if(thedate.after(todate))
            System.out.println("-------------------------->"+ thedate.toString());
        }*/
        FileInputStream file = new FileInputStream(new File(TEST_XML));
      DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
      DocumentBuilder builder =  builderFactory.newDocumentBuilder();
      org.w3c.dom.Document xmlDocument = builder.parse(file);
      XPathFactory xPathFactory = XPathFactory.newInstance();
      XPath xpath = xPathFactory.newXPath();
      XPathExpression xPathExpr = xpath.compile("//AuditRecord/DateTimeStamp/text()");
      Object result = xPathExpr.evaluate(xmlDocument, XPathConstants.NODESET);
      print(result);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (SAXException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    } catch (XPathExpressionException e) {
        e.printStackTrace();
    }       
}   
public static void print(Object result){
NodeList nodes = (NodeList) result;
for (int i = 0; i < nodes.getLength(); i++) {
  System.out.println(i+" "+nodes.item(i).getNodeValue());
}
}

}

2 个答案:

答案 0 :(得分:0)

您的网络应用可能无法访问C:/test.xml,因为通常不允许网络应用直接访问文件系统。

答案 1 :(得分:0)

类路径中存在xmlparserv-2.0.jar是一个问题,它不允许应用xpath表达式。关于如何继续前进的任何想法