0

我有以下 XML 代码

<?xml version="1.0" encoding="windows-1250"?>
<menu>
  <item>
    <id>0</id>
    <name>Pizzerie u soudu</name>
    <street>Havlíčkova 3</street>
    <city>779 00 Olomouc</city>
    <mobile>777035862</mobile>
    <telephone>585223042</telephone>
    <openinghours>10-22</openinghours>
  </item>
  <item>
    <id>1</id>
    <name>Pepinova pizza</name>
    <street>©meralova 10</street>
    <city>779 00 Olomouc</city>
    <mobile>776102022</mobile>
    <telephone>-</telephone>
    <openinghours>8-22</openinghours>
  </item>
</menu>

我想通过以下代码解析它:

XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL);

Document doc = parser.getDomElement(xml);
NodeList nl = doc.getElementsByTagName("item");

        for (int i = 0; i < nl.getLength(); i++) {
            HashMap<String, String> map = new HashMap<String, String>();
            Element e = (Element)nl.item(i);
            map.put(KEY_ID, parser.getValue(e, KEY_ID));
            map.put(KEY_NAME, parser.getValue(e, KEY_NAME));
            map.put(KEY_STREET, parser.getValue(e, KEY_STREET));
            map.put(KEY_CITY, parser.getValue(e, KEY_CITY));
            map.put(KEY_MOBILE, parser.getValue(e, KEY_MOBILE));
            map.put(KEY_TELEPHONE, parser.getValue(e, KEY_TELEPHONE));
            map.put(KEY_OPENINGHOURS, parser.getValue(e, KEY_OPENINGHOURS));
            menuItems.add(map);
        }

但我在网上有问题

NodeList nl = doc.getElementsByTagName("item");

它返回有java.lang.NullPointerException 什么问题?

4

2 回答 2

0

在我看来,您XMLParser的代码是您的专有代码,并且getDomElement方法由于某种原因返回 NULL,例如在路径中找不到文件或其他一些错误。即使是标准 API,请检查是否有任何条件将文档值返回为 NULL。

于 2012-09-27T10:01:50.560 回答
0
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class parsing {
public  void parse() {

try {

File file = new File("F:\\JavaProjects\\JavaApplication12\\src\\javaapplication12\\urdoc.xml");

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(file);
doc.getDocumentElement().normalize();

NodeList nodes = doc.getElementsByTagName("item");

String id,name,street,city,mo,tel,oh;
for (int s = 0; s < nodes.getLength(); s++) {

Node fstNode = nodes.item(s);

System.out.println("Node.ELEMENT_NODE:"+Node.ELEMENT_NODE);
if (fstNode.getNodeType() == Node.ELEMENT_NODE) {
     Element element = (Element) nodes.item(0);
NodeList idp= element.getElementsByTagName("id").item(0).getChildNodes();
id = ((Node) idp.item(0)).getNodeValue();

NodeList np= element.getElementsByTagName("name").item(0).getChildNodes();
name = ((Node) np.item(0)).getNodeValue();

NodeList sp= element.getElementsByTagName("street").item(0).getChildNodes();
street = ((Node) sp.item(0)).getNodeValue();


NodeList cp = element.getElementsByTagName("city").item(0).getChildNodes();
city = ((Node) cp.item(0)).getNodeValue();

NodeList mp = element.getElementsByTagName("mobile").item(0).getChildNodes();
mo= ((Node) mp.item(0)).getNodeValue();    

NodeList tp = element.getElementsByTagName("telephone").item(0).getChildNodes();
tel= ((Node) tp.item(0)).getNodeValue();   

NodeList op= element.getElementsByTagName("openinghours").item(0).getChildNodes();
oh= ((Node) op.item(0)).getNodeValue();   





   System.out.println("After parsing..");
   System.out.println("id "+id);
   System.out.println("name "+name);
   System.out.println("city "+city);
   System.out.println("street "+street);
   System.out.println("telephone "+tel);
   System.out.println(" mobile"+tel);
   System.out.println(" oh"+oh);


    }

}
}
 catch (Exception e)
  { 
  System.out.print("Exception: "+e);

}
}
}
于 2012-09-27T10:28:37.850 回答