Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package xml_data;
- import java.io.File;
- import org.w3c.dom.Document;
- import org.w3c.dom.NodeList;
- import org.w3c.dom.Node;
- import org.w3c.dom.Element;
- import java.io.File;
- import javax.xml.parsers.DocumentBuilder;
- import javax.xml.parsers.DocumentBuilderFactory;
- public class Bookk {
- static String title="Test";
- static String author="Testowy";
- static int date=123;
- public static boolean getFromXmlFile(String path)
- {
- boolean success = false;
- try {
- File fXmlFile = new File(path);
- DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
- DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
- Document doc = dBuilder.parse(fXmlFile);
- doc.getDocumentElement().normalize();
- NodeList nList = doc.getElementsByTagName("book");
- Node nNode = nList.item(0);
- if (nNode.getNodeType() == Node.ELEMENT_NODE) {
- Element db = (Element) nNode;
- title = db.getElementsByTagName("title").item(0).getTextContent();
- author = db.getElementsByTagName("author").item(0).getTextContent();
- date = Integer.parseInt(db.getElementsByTagName("date").item(0).getTextContent());
- success = true;
- }
- } catch (Exception e)
- {
- success = false;
- }
- System.out.println("Tytuł: "+title+" Autor: "+author+" Data: "+date);
- return success;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement