Saturday, 18 April 2015

updating xml from java



I have a small problem with writing new data in an already existing xml file from java. It is my first try with xml files, so I'm not too familiar with them and I don't really understand why is this happening. Matter of fact is that I call the function which should update the file, but nothing happens with it.


Here is the syntax of my xml file:



<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book genre="COOKING">
<title>Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<quantity>5</quantity>
<price>30.00</price>
</book>
<book genre="CHILDREN">
<title>Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<quantity>7</quantity>
<price>29.99</price>
</book>
<book genre="WEB">
<title>Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<quantity>15</quantity>
<price>39.95</price>
</book>
</bookstore>


I tried the solution from here: How do I append a node to an existing XML file in java, accompanied with this one: http://ift.tt/1cGspWw, but it's still not modifying anything.


Here is my code:



public void createBook(String title, String author, int year, String genre, int quantity, int price)
{
try
{
File xmlBookFile = new File(xmlBook);
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document doc = documentBuilder.parse(xmlBookFile);
doc.getDocumentElement().normalize();
Element rootElement = doc.getDocumentElement();
Node bookNode = createNode(doc);
rootElement.appendChild(bookNode);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File(xmlBook));
transformer.transform(source, result);
}
catch (Exception e)
{
e.printStackTrace();
}
}

public Node createNode(Document doc)
{
Element bookElement = doc.createElement("book");
Attr genreAttribute = doc.createAttribute("genre");
genreAttribute.setValue(genre);
Element titleElement = doc.createElement("title");
titleElement.appendChild(doc.createTextNode(title));
Element authorElement = doc.createElement("author");
authorElement.appendChild(doc.createTextNode(author));
Element yearElement = doc.createElement("year");
yearElement.appendChild(doc.createTextNode(Integer.toString(year)));
Element quantityElement = doc.createElement("quantity");
quantityElement.appendChild(doc.createTextNode(Integer.toString(quantity)));
Element priceElement = doc.createElement("price");
priceElement.appendChild(doc.createTextNode(Integer.toString(price)));
bookElement.setAttributeNode(genreAttribute);
bookElement.appendChild(titleElement);
bookElement.appendChild(authorElement);
bookElement.appendChild(yearElement);
bookElement.appendChild(quantityElement);
bookElement.appendChild(priceElement);
return bookElement;
}


I would appreciate every bit of help why isn't this updating my file and what do I do wrong. Thank you!


No comments:

Post a Comment