Advertisement
Guest User

Untitled

a guest
May 18th, 2010
525
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. public void parseXML(String xml) {
  2.  
  3. try {
  4. InputStream inputStream = new ByteArrayInputStream(xml.getBytes());
  5. DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  6. DocumentBuilder db = dbf.newDocumentBuilder();
  7. Document doc = db.parse(inputStream);
  8. doc.getDocumentElement().normalize();
  9. System.out.println("Root element " + doc.getDocumentElement().getNodeName());
  10. NodeList nodeLst = doc.getElementsByTagName("entry");
  11. System.out.println("Information of all entries");
  12.  
  13. for (int s = 0; s < nodeLst.getLength(); s++) {
  14.  
  15. Node fstNode = nodeLst.item(s);
  16.  
  17. if (fstNode.getNodeType() == Node.ELEMENT_NODE) {
  18.  
  19. Element fstElmnt = (Element) fstNode;
  20.  
  21. NodeList title = fstElmnt.getElementsByTagName("title").item(0).getChildNodes();
  22. System.out.println("Title : " + ((Node) title.item(0)).getNodeValue());
  23.  
  24. NodeList id = fstElmnt.getElementsByTagName("id").item(0).getChildNodes();
  25. System.out.println("Id: " + ((Node) id.item(0)).getNodeValue());
  26.  
  27. NodeList contento= fstElmnt.getElementsByTagName("tempi").item(0).getChildNodes();
  28. System.out.println("Contento : " + ((Node) contento.item(1)).getFirstChild().getAttributes());
  29. // with this line above I'm having problems, I get null pointer exception
  30. }
  31.  
  32. }
  33. } catch (Exception e) {
  34. e.printStackTrace();
  35. }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement