XML文件从netbeans中解析成功,但从可运行的jar中解析失败



我开发了一个代码,解析xml文件,并在netbeans的框架中返回结果。代码从netbeans成功运行,但当将项目导出到jar文件时,它没有显示任何东西。请,如果你有想法,我会很感激,如果你帮助我。这里是我用于解析的代码。

import java.io.IOException;
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.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;
public class Parsing {

String a = null;
public static void main(String a) throws
        ParserConfigurationException, SAXException, IOException,
        XPathExpressionException {
    a = getElem(a);
}
public static String getElem(String a) throws ParserConfigurationException,   SAXException, XPathExpressionException, IOException {
    String file = "src/xml/read.xml";

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(false);
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.parse(file);
    XPath xpath = XPathFactory.newInstance().newXPath();
    Node CustomerId = (Node) xpath.evaluate("//Operation[@name='Read' and @modifier='Customer']/ParameterList/StringParameter[@name='CustomerId']/text()",
            doc.getDocumentElement(), XPathConstants.NODE);
    a = CustomerId.getNodeValue();
    return a;
   }
 }

当从另一个框架调用getElem(a)方法时,它在文本框中显示a的值,但是当将项目导出到jar文件时,它不显示任何东西!

让我印象深刻的第一件事是使用XML资源的相对路径。似乎您依赖于相对于您的工作目录的目录中存在的文件。

src目录在运行时不存在。这表明XML文件是一个嵌入式资源,因此,不能像文件系统上的文件那样被访问。

你应该使用像…

getClass().getResourceAsStream("/xml/read.xml")

并将得到的InputStream传递给DocumentBuilder。别忘了关闭

下面是使用jar文件的修改后的代码:)

  import java.io.IOException;
  import java.io.InputStream;
  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.XPathExpressionException;
  import javax.xml.xpath.XPathFactory;
  import org.w3c.dom.Document;
  import org.w3c.dom.Node;
  import org.xml.sax.InputSource;
  import org.xml.sax.SAXException;

  public class Parsing {

String a = null;
private static final String TEST_XML = "/xml/read.xml";
public static void main(String a) throws
        ParserConfigurationException, SAXException, IOException,
        XPathExpressionException {
    a = getElem(a);
}
protected static InputSource getTestXMLInputSource() {
    InputStream is = Parsing.class.getResourceAsStream(TEST_XML);

    is = Parsing.class.getResourceAsStream(TEST_XML);
    return new InputSource(is);
}
public static String getElem(String a) throws ParserConfigurationException, SAXException, XPathExpressionException, IOException {

    final InputSource source = getTestXMLInputSource();
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(false);
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.parse(source);

    XPath xpath = XPathFactory.newInstance().newXPath();
    Node CustomerId = (Node) xpath.evaluate("//Operation[@name='Read' and @modifier='Customer']/ParameterList/StringParameter[@name='CustomerId']/text()",
            doc.getDocumentElement(), XPathConstants.NODE);
    a = CustomerId.getNodeValue();
    return a;
   }
  }

相关内容

  • 没有找到相关文章

最新更新