piowit

parsingxmlfinal

Nov 10th, 2012
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.03 KB | None | 0 0
  1. import javax.xml.parsers.*;
  2. import org.w3c.dom.*;
  3.  
  4.  
  5.  
  6. public class Test
  7. {
  8.         public static void main(String[] args) throws Exception{
  9.            
  10. //          IOMethods.convertToXML();
  11.            
  12.             DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  13.             DocumentBuilder builder = factory.newDocumentBuilder();
  14.            
  15. //          Document doc = builder.parse("temp.xml");
  16.             Document doc = builder.parse("http://www.pogodynka.net/api:server/weather/getCurrent.xml?city=Warszawa");
  17.            
  18.             Element root = doc.getDocumentElement();
  19.            
  20. //          NamedNodeMap
  21.  
  22.             NodeList children = root.getChildNodes();
  23.             for (int i= 0; i < children.getLength(); i++)
  24.             {
  25.                 Node child = children.item(i);
  26.                 if (child instanceof Element)
  27.                 {
  28.                     Element childElement = (Element)child;
  29.  
  30.                     Text textNode = (Text) childElement.getFirstChild();
  31.                     String text = textNode.getData().trim();
  32.                     System.out.println(childElement.getNodeName() + ": " + text);
  33.                 }
  34.             }
  35.            
  36.                
  37.            
  38.         }  
  39.        
  40.  
  41.        
  42. }
Advertisement
Add Comment
Please, Sign In to add comment