import java@H_404_2@.io@H_404_2@.IOException;
import javax@H_404_2@.xml@H_404_2@.parsers@H_404_2@.DocumentBuilder;
import javax@H_404_2@.xml@H_404_2@.parsers@H_404_2@.DocumentBuilderFactory;
import javax@H_404_2@.xml@H_404_2@.parsers@H_404_2@.ParserConfigurationException;
import org@H_404_2@.w3c@H_404_2@.dom@H_404_2@.Document;
import org@H_404_2@.w3c@H_404_2@.dom@H_404_2@.Element;
import org@H_404_2@.w3c@H_404_2@.dom@H_404_2@.Node;
import org@H_404_2@.w3c@H_404_2@.dom@H_404_2@.NodeList;
import org@H_404_2@.xml@H_404_2@.sax@H_404_2@.SAXException;
public class sddddddddddddd {
public static void main(String[] args) throws ParserConfigurationException,SAXException,IOException {
// TODO Auto-generated method stub
DocumentBuilderFactory dbf = DocumentBuilderFactory@H_404_2@.newInstance();
//从DOM工厂获得解析器
DocumentBuilder db = dbf@H_404_2@.newDocumentBuilder();
//获得DOM树,解析出来文档
Document doc=db@H_404_2@.parse("D:\\Phone.xml");
//获得节点信息
System@H_404_2@.out@H_404_2@.println("hh");
NodeList brandlist = doc@H_404_2@.getElementsByTagName("Brand");
for (int i=0;i<brandlist.getLength();i++){
System@H_404_2@.out@H_404_2@.println("hh");
Node brand = brandlist@H_404_2@.item(i);
Element element = (Element) brand;
String attrValue = element@H_404_2@.getAttribute("name");
NodeList types = element@H_404_2@.getChildNodes();
System@H_404_2@.out@H_404_2@.println("hh");
for (int j=0;j<types.getLength();j++){
Element typelement = (Element) types@H_404_2@.item(j);
String type = typelement@H_404_2@.getAttribute("name");
System@H_404_2@.out@H_404_2@.println(attrValue+type);
System@H_404_2@.out@H_404_2@.println("hh");
}
}
}
}
原因在于,XML中间不能有空格,在网络上也有很多人指出了这个问题。
<?xml version="1.0" encoding="utf-8" ?>
<phone><Brand name="华为"><Type name="mate7"/><Type name="P7"/><Type name="荣耀7"/></Brand><Brand name="苹果"><Type name="Iphone5"/><Type name="Iphone6"/><Type name="Iphone7"/></Brand></phone>
如果有空格或者回车存在,就会导致错误的解析,API认为也是一个节点,结果解析出来什么都没有,就是null