Java: Function that parses XML data - nothing being outputted



I'm relatively new to XML processing with Java, so expect some mistakes, but anyway...I'm trying to parse the following XML data:


http://ift.tt/1jtIGN3


I would like to accomplish this using a function, where the name of the XML tag and NodeList are passed in as parameters, and it returns the content.


Thanks.



import java.io.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;

public class Files {

@SuppressWarnings("unused")

public static void main (String [] args) throws IOException, ParserConfigurationException, SAXException{

String address = "/home/leo/workspace/Test/Files/src/file.xml";

String author = "author";
String title = "title";
String genre = "genre";
String price = "price";
String publish = "publish_date";
String descr = "description";


File xmlFile = new File(address);

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = factory.newDocumentBuilder();
Document doc = dBuilder.parse(xmlFile);

doc.getDocumentElement().normalize();

System.out.println(doc.getDocumentElement().getNodeName());
NodeList n = doc.getElementsByTagName("book");
System.out.println("Number of shows " + n.getLength());

getElement(author, n);

}

private static void getElement(String elementName, NodeList n){

for (int i = 0; i < n.getLength(); i++){
Node showNode = n.item(i);

Element showElement = (Element)showNode;

System.out.println(elementName + ": " +
showElement.getAttribute(elementName)

);

}

}

}

No comments:

Post a Comment