10 Kasım 2013 Pazar


1.Yol

  /*  
   * To change this template, choose Tools | Templates  
   * and open the template in the editor.  
   */  
  package main;  
  /**  
   *  
   * @author Hasan Çelik  
   */  
  import javax.xml.parsers.DocumentBuilderFactory;  
  import javax.xml.parsers.DocumentBuilder;  
  import org.w3c.dom.Document;  
  import org.w3c.dom.NodeList;  
  import org.w3c.dom.Node;  
  import org.w3c.dom.Element;  
  import java.io.StringReader;  
  import org.xml.sax.InputSource;  
  public class ReadXMLFile {  
    public static void main(String argv[]) {  
      try {  
        //File xmlFile = new File("/Users/hasan/belge.xml");  //dilerseniz xml dosyasındanda okuma işlemi gerçekleştirebilirsiniz.  
        String xml = "\n"  
            + "\n"  
            + " \n"  
            + "  200\n"  
            + "  İşlem başarılı\n"  
            + " \n"  
            + "";  
        InputSource source = new InputSource(new StringReader(xml));  
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();  
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();  
        Document doc = dBuilder.parse(source);  
        doc.getDocumentElement().normalize();  
        NodeList liste = doc.getElementsByTagName("status");  
        for (int temp = 0; temp < liste.getLength(); temp++) {  
          Node node = liste.item(temp);  
          System.out.println("Root element :" + doc.getDocumentElement().getNodeName());  
          System.out.println("Element :" + node.getNodeName());  
          if (node.getNodeType() == Node.ELEMENT_NODE) {  
            Element eElement = (Element) node;  
            System.out.println("message : " + eElement.getElementsByTagName("message").item(0).getTextContent());  
            System.out.println("code : " + eElement.getElementsByTagName("code").item(0).getTextContent());  
          }  
        }  
      } catch (Exception e) {  
        e.printStackTrace();  
      }  
    }  
  }

Sonuç

  run:  
  Root element :response  
  Element :status  
  message : İşlem başarılı  
  code : 200  
  BUILD SUCCESSFUL (total time: 0 seconds)  

2.Yol

  /*  
   * To change this template, choose Tools | Templates  
   * and open the template in the editor.  
   */  
  package main;  
  import java.io.StringReader;  
  import javax.xml.parsers.DocumentBuilder;  
  import javax.xml.parsers.DocumentBuilderFactory;  
  import javax.xml.xpath.XPath;  
  import javax.xml.xpath.XPathFactory;  
  import org.w3c.dom.Document;  
  import org.xml.sax.InputSource;  
  /**  
   *  
   * @author Hasan Çelik  
   */  
  public class ReadXmlFile2 {  
    public static void main(String argv[]) {  
      String message;  
      String code;  
      try {  
        //File xmlFile = new File("/Users/hasan/belge.xml");  //dilerseniz xml dosyasındanda okuma işlemi gerçekleştirebilirsiniz.  
        String xml = "\n"  
            + "\n"  
            + " \n"  
            + "  200\n"  
            + "  İşlem başarılı\n"  
            + " \n"  
            + "";  
        InputSource source = new InputSource(new StringReader(xml));  
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();  
        DocumentBuilder db = dbf.newDocumentBuilder();  
        Document document = db.parse(source);  
        XPathFactory xpathFactory = XPathFactory.newInstance();  
        XPath xpath = xpathFactory.newXPath();  
        message = xpath.evaluate("/response/status/message", document);  
        code = xpath.evaluate("/response/status/code", document);  
        System.out.println("message = " + message);  
        System.out.println("code = " + code);  
      } catch (Exception e) {  
        System.out.println(e.getMessage());  
      }  
    }  
  } 

Sonuç

  run:  
  message = İşlem başarılı  
  code = 200  
  BUILD SUCCESSFUL (total time: 0 seconds) 

3 yorum :

  1. Merhabalar,
    Aradan 3 yıl geçmiş farkındayım ama bilginin yaşı,yeri,zamanı yoktur,yazınızı yeni okudum elinize sağlık fakat ben bu işlemi direkt olarak url den yapmak istiyorum ki bilgiler hep güncel kalsınlar.Rica etsem bunu bana açıklaya bilir misiniz.
    Url'den Xml dosyası alıp parse edip ekrana yazdırmak istiyorum.(xml hava durumu dosyası)

    YanıtlaSil
  2. Daha doğrusu nasıl yapılabileceğini anlatabilirseniz sevinirim.

    Hayırlı akşamlar.

    YanıtlaSil
    Yanıtlar
    1. http://stackoverflow.com/questions/22487516/how-to-read-xml-document-from-url

      http://stackoverflow.com/questions/2310139/how-to-read-xml-response-from-a-url-in-java

      Konuyla ilgili yukarıda paylaştığım linklerden yararlanabilirsiniz. Saygılar.

      Sil