2010-04-09 5 views
0

J'ai un fichier XML de type html, il s'agit essentiellement de html. J'ai besoin d'avoir les éléments dans chacun. Chaque élément ressemble à ceci:java: comment analyser un fichier XML de type html

<line tid="744476117"> <attr>1414</attr> <attr>31</attr><attr class="thread_title">title1</attr><attr>author1</attr><attr>date1</attr></line> 

Mon code est comme ci-dessous, il ne reconnaît qu'il ya 50 dans le fichier, mais il me donne NULLPointException lors de l'analyse NodeList fstNmElmntLst = fstElmnt.getElementsByTagName("attr");

Toute idée pourquoi cela se passe? Le même code a été utilisé pour d'autres applications sans problèmes.

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
DocumentBuilder db = dbf.newDocumentBuilder(); 
InputSource is = new InputSource(); 
is.setCharacterStream(new StringReader(cleanxml)); 
Document doc = db.parse(is);     
doc.getDocumentElement().normalize(); 
System.out.println("Root element " + doc.getDocumentElement().getNodeName()); 
NodeList nodeLst = doc.getElementsByTagName("line"); 
for (int s = 0; s < nodeLst.getLength(); s++) { 
System.out.println(nodeLst.getLength()); 
Node fstNode = nodeLst.item(s); 
if (fstNode.getNodeType() == Node.ELEMENT_NODE) { 
               Element fstElmnt = (Element) fstNode; 
    NodeList fstNmElmntLst = fstElmnt.getElementsByTagName("attr"); 
    Element fstNmElmnt = (Element) fstNmElmntLst.item(0); 
     NodeList fstNm = fstNmElmnt.getChildNodes(); 
    System.out.println("attr : " + ((Node) fstNm.item(0)).getNodeValue()); 
} 
} 

Répondre

2

problème résolu! Un des <line> n'a aucun <attr> qui cause ce problème!