无法使用XML文件。 InputStream为null [英] Cannot work with XML file. InputStream is null

查看:2091
本文介绍了无法使用XML文件。 InputStream为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里做错了什么?我收到错误,输入流中没有任何内容,但事实并非如此。文件在那里,标题正确。我想抓住我放在XML文件中的IP地址。有没有更好的方法来解析文件而不是 dBuilder.parse(XMLReader.class.getResourceAsStream(C:\\Tools \\CLA \\ test.xml)) );

What am I doing wrong here? I am getting the error there there is nothing in the input stream when that is not the case. The file is there and it is titled correctly. I want to grab the ip addresses i have put in my XML file. Is there a better way to parse in the file instead of dBuilder.parse(XMLReader.class.getResourceAsStream("C:\\Tools\\CLA\\test.xml")); ?

我遇到了这个例外:

Caused by: java.lang.IllegalArgumentException: InputStream cannot be null
    at javax.xml.parsers.DocumentBuilder.parse(Unknown Source)
    at com.Intel.ameier.XMLparser.TryXML(XMLparser.java:17)

以下是代码:

import java.io.IOException;
import org.xml.sax.*;
import org.w3c.dom.*;
import javax.xml.parsers.*;

public class XMLparser {

    protected void TryXML() throws ParserConfigurationException, SAXException, IOException{

        DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();

        DocumentBuilder dBuilder = builderFactory.newDocumentBuilder();
        Document document = dBuilder.parse(XMLReader.class.getResourceAsStream("C:\\Tools\\test.xml"));
        document.normalize();

        NodeList rootNodes = document.getElementsByTagName("info");
        Node rootNode = rootNodes.item(0);
        Element rootElement = (Element)rootNode;
        NodeList compList = rootElement.getElementsByTagName("computer");
        for(int i = 0;i < compList.getLength(); i++){

            Node computer = compList.item(i);
            Element compElement = (Element)computer;

            Node theIP = compElement.getElementsByTagName("ipaddress").item(0);
            Element theIpElement = (Element)theIP;

            System.out.println("The comptuer ip is : " + theIpElement.getTextContent());


        }


    }


}

XML文件:

<?xml version="1.0"?>
<info>
    <testsuite>
        <name></name>
    </testsuite>
    <computer>
        <ipaddress>111.11.11.6</ipaddress>
    </computer>
    <computer>
        <ipaddress>111.11.11.5</ipaddress>
    </computer>
    <computer>
        <ipaddress>111.11.11.3</ipaddress>
    </computer>
</info>


推荐答案

这与XML无关。你正在使用 Class.getResourceAsStream ,这是为了从该类的类加载器的类路径中加载一个资源...但是你要传递一个 filename 而不是。

This has nothing to do with XML. You're using Class.getResourceAsStream, which is meant to get a resource to be loaded from the classpath for that class's class loader... but you're passing in a filename instead.

如果要为文件创建输入流,只需使用 FileInputStream

If you want to create an input stream for a file, just use FileInputStream:

Document document = dBuilder.parse(new FileInputStream("C:\\Tools\\test.xml"));

或者更好,为了关闭流:

Or better, in order to close the stream:

Document document:

try (InputStream stream = new FileInputStream("C:\\Tools\\test.xml")) {
    document = dBuilder.parse(stream);
}

这篇关于无法使用XML文件。 InputStream为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆