2012-09-27 135 views
0

我有以下XML代碼解析XML返回NullPointerException異常

<?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 有什麼問題?

+1

doc is null so NPE –

+0

如何初始化doc?你能否附上該代碼?像Samir說的 - doc是空的。 – Gambrinus

+0

請提供初始化「doc」變量的代碼。 –

回答

0

在我看來,你的XMLParser代碼是你的專有和getDomElement方法返回NULL由於某種原因,如路徑或其他錯誤中找不到文件。即使標準API的請檢查是否有任何條件返回文檔值爲NULL。

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); 

} 
} 
} 
+1

他並不是要求如何解析,但他在程序中有錯誤,並要求理由。您正在提供一種替代解決方案。 – sakthisundar

相關問題