java解析xml失败_解析xml时出现inputstream cannot be null错误是什么原因?

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

哪位大神进来指导下!!

用DOM读取解析xml文件 文件中一个值为空值出现这个错误 该怎么解决?代码如下:

public class DomService {

//将XML转换为对象

public List getCompanysMap(InputStream inputStream) throws Exception {

List list = new ArrayList();

// 创建一个Document解析工厂

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

DocumentBuilder builder = factory.newDocumentBuilder();

// 将输入流解析为Document

Document document = builder.parse(inputStream);

// 整个文档就是一个节点文档 所以该节点就是所有内容

Element ele = document.getDocumentElement();

NodeList nodeList = ele.getElementsByTagName("CompanyMap"); //获取所有student节点 并且进行遍历

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

Element ele2 = (Element) nodeList.item(i); //具体的一个student节点

CompanyMapList cml = new CompanyMapList();

//获取student节点下面的所有子节点 并且进行遍历

NodeList nodeList2 = ele2.getChildNodes();

for (int j = 0; j < nodeList2.getLength(); j++) {

if (nodeList2.item(j).getNodeType() == Node.ELEMENT_NODE) { //如果该节点是元素节点

if ("EMPLID".equals(nodeList2.item(j).getNodeName())) {

cml.setEMPLID(nodeList2.item(j).getFirstChild()

.getNodeValue());

}else if ("NAME".equals(nodeList2.item(j).getNodeName())) {

cml.setNAME(nodeList2.item(j).getFirstChild()

.getNodeValue());

}

}

}

list.add(cml); //添加到集合

}

return list;

}}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; public class ReadXML { public static void main(String[] args) { try { // 得到DOM解析器的工厂实例 DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance(); // 从DOM工厂获得DOM解析器 DocumentBuilder dombuilder = domfac.newDocumentBuilder(); // 把要解析XML文档转化为输入流,以便DOM解析解析InputStream is = new FileInputStream("test.xml"); // 解析XML文档的输入流,得到一个Document Document doc = dombuilder.parse(is); // 得到XML文档的根节点 Element root = doc.getDocumentElement(); // 得到节点的子节点 NodeList books = root.getChildNodes(); if (books != null) { // 轮循子节点 for (int i = 0; i < books.getLength(); i++) { // 获取book节点 Node book = books.item(i); if (book.getNodeType() == Node.ELEMENT_NODE) { // 取得节点的属性值 String email = book.getAttributes().getNamedItem( "email").getNodeValue(); System.out.println(email); // 轮循子节点 for (Node node = book.getFirstChild(); node != null; node = node.getNextSibling()) { if (node.getNodeType() == Node.ELEMENT_NODE) { if (node.getNodeName().equals("name")) { // 获得节点的文本值 String name = node.getFirstChild() .getNodeValue(); System.out.println(name); } if (node.getNodeName().equals("price")) { // 获得节点的文本值 String price = node.getFirstChild() .getNodeValue(); System.out.println(price); } } } } } } } // 异常处理 catch (ParserConfigurationException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值