1

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..
    }
}
dtgee
  • 1,272
  • 2
  • 15
  • 30
  • Can you be more specific about what it is you're trying to do? Why will a Java object with nullable fields not suffice for the representation? – chrylis -cautiouslyoptimistic- Feb 19 '14 at 07:43
  • If `` can contain, say, 30 different tags, and you need to handle all the ones that are present in any XML file, then you cannot avoid writing code for each case. Your question isn't specific enough to clearly show what you're trying to accomplish. Besides, once you've asked the XML parser to build the DOM, you're done parsing. All that's left is to process what you want. – Jim Garrison Feb 19 '14 at 07:43
  • Not sure what you are trying to do, but may be XSLT bears examining. – Jim Garrison Feb 19 '14 at 07:45
  • Sorry, I reedited my question to make it more clear. Let me know if anything else is still confusing. – dtgee Feb 19 '14 at 07:49
  • Possibly create your own data type to capture the elements you want and the state of each would be defined by your xml elements, take a look at [this example](http://stackoverflow.com/questions/21534340/using-java-to-parse-xml/21713344#21713344) which I had proposed earlier, and the entire thread – StoopidDonut Feb 19 '14 at 08:22
  • you could use something like the Jackson XmlMapper to handle this for you. You create a simnple bean and annotate it so properties are optional. You can then turn the xml into beans with a couple of lines of code. – tom Feb 19 '14 at 09:56

0 Answers0