- import java.io.IOException;
- import javax.xml.parsers.*;
- import java.io.*;
- import javax.xml.parsers.DocumentBuilderFactory;
- import org.apache.xml.serialize.*;
- import org.w3c.dom.*;
- import org.xml.sax.SAXException;
- public class XMLAssignment1 {
- static Document dom;
- static Document domNew;
- public static void main(String[] args) {
- printStuff("Creating empty DOM document....");
- createDocument();
- printStuff("Opening file and parsing XML");
- String xmlFile = "bookstore.xml";
- parseXMLFile(xmlFile);
- printStuff("Transversing XML file...");
- printStuff("Creating Nodes....Appending....");
- createDOMTree();
- printStuff("Writing to file");
- printToFile();
- printStuff("File book.xml");
- }
- public static void printStuff(String s) {
- System.out.println(s);
- }
- private static void parseXMLFile(String fileName) {
- DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
- try {
- DocumentBuilder db = dbf.newDocumentBuilder();
- dom = db.parse(fileName);
- }
- catch(ParserConfigurationException e) {
- e.printStackTrace();
- }
- catch(SAXException e) {
- e.printStackTrace();
- }
- catch(IOException e) {
- e.printStackTrace();
- }
- }
- private static String getTextValue(Element e, String tag) {
- String textVal = null;
- NodeList nl = e.getElementsByTagName(tag);
- if (nl != null && nl.getLength() > 0) {
- Element el = (Element)nl.item(0);
- textVal = el.getFirstChild().getNodeValue();
- }
- return textVal;
- }
- private static int getIntValue(Element e, String tag) {
- return Integer.parseInt(getTextValue(e,tag));
- }
- private static void createDocument() {
- //get an instance of factory
- DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
- try {
- //get an instance of builder
- DocumentBuilder db = dbf.newDocumentBuilder();
- //create an instance of DOM
- domNew = db.newDocument();
- //DomNew is a global
- }
- catch(ParserConfigurationException pce) {
- //dump it
- System.out.println("Error while trying to instantiate DocumentBuilder " + pce);
- System.exit(1);
- }
- }
- /**
- * creates the XML structure
- */
- private static void createDOMTree() {
- String textVal;
- Text t;
- //create the root element
- Element rootEle = domNew.createElement("bookstore");
- domNew.appendChild(rootEle);
- Element docRoot = dom.getDocumentElement();
- NodeList nodeList = docRoot.getElementsByTagName("book");
- //Check to make sure nodeList isn't empty
- if (nodeList != null && nodeList.getLength() > 0) {
- //We need to loop all the books
- for(int i=0; i < nodeList.getLength(); i++) {
- //Gets book node
- Element book = (Element)nodeList.item(i);
- //Create new node
- Element newEl = domNew.createElement("book");
- //Add Attribute
- newEl.setAttribute("category",book.getAttribute("category"));
- //search book node for all authors
- NodeList authors = book.getElementsByTagName("author");
- for (int j=0; j< authors.getLength(); j++) {
- //Create Author node
- Element newAuthorEl = domNew.createElement("author");
- //List of author nodes
- Element authorNodes = (Element)authors.item(j);
- textVal = authorNodes.getFirstChild().getNodeValue();
- //printStuff("Author " + j + " : " + textVal);
- t = domNew.createTextNode(textVal);
- newAuthorEl.appendChild(t);
- //Append authornode to bookNode
- newEl.appendChild(newAuthorEl);
- }
- // We know that we have title, year, and price in any order
- Element newTitleEl = domNew.createElement("title");
- NodeList orgTitle = book.getElementsByTagName("title");
- Element orgTitleEl = (Element)orgTitle.item(0);
- newTitleEl.setAttribute("lang", orgTitleEl.getAttribute("lang"));
- t = domNew.createTextNode(orgTitleEl.getFirstChild().getNodeValue());
- newTitleEl.appendChild(t);
- //getPrice
- Element newPriceEl = domNew.createElement("price");
- Element orgPrice = (Element)book.getElementsByTagName("price").item(0);
- t = domNew.createTextNode(orgPrice.getFirstChild().getNodeValue());
- newPriceEl.appendChild(t);
- //getYear
- Element newYearEl = domNew.createElement("year");
- Element orgYear = (Element)book.getElementsByTagName("year").item(0);
- t = domNew.createTextNode(orgYear.getFirstChild().getNodeValue());
- newYearEl.appendChild(t);
- //Write children to book
- newEl.appendChild(newTitleEl);
- newEl.appendChild(newPriceEl);
- newEl.appendChild(newYearEl);
- //Append bookNode to bookstore
- rootEle.appendChild(newEl);
- }
- }
- }
- @SuppressWarnings("deprecation")
- private static void printToFile(){
- try
- {
- //print
- OutputFormat format = new OutputFormat(domNew);
- format.setIndenting(true);
- //to generate output to console use this serializer
- //XMLSerializer serializer = new XMLSerializer(System.out, format);
- //to generate a file output use fileoutputstream instead of system.out
- XMLSerializer serializer = new XMLSerializer(
- new FileOutputStream(new File
- ("book.xml")), format);
- serializer.serialize(domNew);
- }
- catch(IOException ie) {
- ie.printStackTrace();
- }
- }
- }
asd
By: asd | Date: Feb 9 2011 23:06 | Format: None | Expires: never | Size: 5.74 KB | Hits: 898
Latest pastes
8 hours ago
3 days ago
4 days ago
4 days ago
6 days ago