I'm trying to parse an XML file and insert into MySQL all the data that I've parsed. However, I don't quite get how to I should parse an XML file with variable fields. I have tried researching for hours on how to do this. From what I've learned, you have to know something about the type of data you're parsing. In this case, I believe I do. Here is the DTD file I am working with: XML format
So if we have an XML file containing data such as this (just as a very simple example):
<book mdate="2004-03-08" key="books/acm/Kim95">
<title>
Modern Database Systems: The Object Model, Interoperability, and Beyond.
</title>
</book>
<book mdate="2007-03-01" key="books/aw/Stonebraker86">
<title>
The INGRES Papers: Anatomy of a Relational Database System
</title>
<url>
db/books/collections/Stonebraker86.html
</url>
</book>
Then I feel like I would need a bunch of if statements and a bunch of null checks to see which fields aren't present since the DTD file specifies a lot of possible fields for each element. However, I feel like this is a really bad way to go about parsing it. I've tried Googling for examples which handles such a case, but all the examples seem to have the same fields for each element.
NodeList nl = docEle.getElementsByTagName("book");
if(nl != null && nl.getLength() > 0) {
for(int i = 0 ; i < nl.getLength();i++) {
//get the book element
Element bl = (Element)nl.item(i);
//get the book's associated fields
String title = getTextValue(b1,"title");
String url = getTextValue(bl,"url");
//and so on and so fourth
//check if null
if (title.equals("")) {
// don't use this as a field to insert into the database
}
if (url.equals("")) {
// don't use this as a field either
}
//and so on..
}
}