Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void parseXML(String xml) {
- try {
- InputStream inputStream = new ByteArrayInputStream(xml.getBytes());
- DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
- DocumentBuilder db = dbf.newDocumentBuilder();
- Document doc = db.parse(inputStream);
- doc.getDocumentElement().normalize();
- System.out.println("Root element " + doc.getDocumentElement().getNodeName());
- NodeList nodeLst = doc.getElementsByTagName("entry");
- System.out.println("Information of all entries");
- for (int s = 0; s < nodeLst.getLength(); s++) {
- Node fstNode = nodeLst.item(s);
- if (fstNode.getNodeType() == Node.ELEMENT_NODE) {
- Element fstElmnt = (Element) fstNode;
- NodeList title = fstElmnt.getElementsByTagName("title").item(0).getChildNodes();
- System.out.println("Title : " + ((Node) title.item(0)).getNodeValue());
- NodeList id = fstElmnt.getElementsByTagName("id").item(0).getChildNodes();
- System.out.println("Id: " + ((Node) id.item(0)).getNodeValue());
- NodeList contento= fstElmnt.getElementsByTagName("tempi").item(0).getChildNodes();
- System.out.println("Contento : " + ((Node) contento.item(1)).getFirstChild().getAttributes());
- // with this line above I'm having problems, I get null pointer exception
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement