Advertisement
Guest User

Untitled

a guest
May 27th, 2015
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.36 KB | None | 0 0
  1. package xml_data;
  2.  
  3. import java.io.File;
  4. import org.w3c.dom.Document;
  5. import org.w3c.dom.NodeList;
  6. import org.w3c.dom.Node;
  7. import org.w3c.dom.Element;
  8.  
  9. import java.io.File;
  10.  
  11. import javax.xml.parsers.DocumentBuilder;
  12. import javax.xml.parsers.DocumentBuilderFactory;
  13.  
  14. public class Bookk {
  15.     static String title="Test";
  16.     static String author="Testowy";
  17.     static int date=123;
  18.    
  19.    
  20.     public static boolean getFromXmlFile(String path)
  21.     {
  22.         boolean success = false;
  23.         try {
  24.             File fXmlFile = new File(path);
  25.             DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
  26.             DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
  27.             Document doc = dBuilder.parse(fXmlFile);
  28.             doc.getDocumentElement().normalize();
  29.             NodeList nList = doc.getElementsByTagName("book");
  30.             Node nNode = nList.item(0);
  31.             if (nNode.getNodeType() == Node.ELEMENT_NODE) {
  32.                 Element db = (Element) nNode;
  33.                 title = db.getElementsByTagName("title").item(0).getTextContent();
  34.                 author = db.getElementsByTagName("author").item(0).getTextContent();
  35.                 date = Integer.parseInt(db.getElementsByTagName("date").item(0).getTextContent());
  36.                 success =  true;
  37.             }
  38.            
  39.         } catch (Exception e)
  40.         {
  41.             success = false;
  42.         }
  43.         System.out.println("Tytuł: "+title+" Autor: "+author+" Data: "+date);
  44.         return success;
  45.     }
  46.  
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement