File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes XML and Related Technologies and the fly likes Using JDOM lib for parssing XML and writing out XML according to a DTD Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Using JDOM lib for parssing XML and writing out XML according to a DTD" Watch "Using JDOM lib for parssing XML and writing out XML according to a DTD" New topic
Author

Using JDOM lib for parssing XML and writing out XML according to a DTD

Mark Hughes
Ranch Hand

Joined: Jul 14, 2006
Posts: 146
Hi Guys,

Hope all are well.
Im using the JDom library to read in and parse data out of an xml file which is compliant to a certain DTD layout. Now based on the data in the xml file i have created 2 Java objects with setters and getters which will store the data i parse out of the xml file. So in the end im left with one object holding certain data, and a list of different objects holding another set of data (The list are all of the same type of object) .

I then perform some processing on the 2 objects, once the processing is done i store them all in another single list of objects, then using this single list i serialise it as XML.

The only thing im unsure of is how to utilise the DTD file. I wish to read in the original xml file and also make sure it is structured as specified in the DTD file. Also above when i mentioned i serilaise the xml, the xml is not in the structure as specified in the DTD. I would like to know how to rite out a list of objects and make sure the seralised xml complies to the structure specified in the DTD file.
Below is some of teh code i use.

Read in the xml file


Parse the xml file looking for parameters



Write the xml to a file



Any help appreciated in terms of helping me get the code to read in and write out XML in the format specified in a DTD

Best
Mark
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18141
    
    8

If you write the XML, and it doesn't conform to the DTD, then that's a bug in your program. You can unit-test that by parsing the XML which you wrote and validating it against the DTD.

I'm not sure whether you mean you want to write generalized code which produces XML which validates against arbitrary DTDs, or whether you want to write code which produces XML which validates against a particular DTD. If it's the latter, just fix your code until the output does validate. If it's the former, I don't have any advice.
Mark Hughes
Ranch Hand

Joined: Jul 14, 2006
Posts: 146
Hi Paul,

I want to write code which produces XML which validates against a particular DTD. As you can see from my writeFile method, it takes a list of objects and a String which contains the location where the xml file should be written.

This works fine, just the xml is in a format as follows:


I would like the outputed xml to appear in the followong format:


I thought maybe it was possible to read in a DTD, and some methods belonging to the JDOm library would be able to change the structure of how the XML is written out. Just currently im using the java class XMLEncoder to write out the XMl and im not sure whether it is possible to specify how the XML should look once written.
Mark Hughes
Ranch Hand

Joined: Jul 14, 2006
Posts: 146
Ok i have decided to manually create the XML in the format i want. Now i juest have a question about adding elements.

My test main code below outputs xml, in the format i want, but all the data is not being outputted. The setAttribute method only allows one value to be set, while i want more than one line of xml to have attributes set.

for example my outputted xml is:
<?xml version="1.0" encoding="UTF-8"?>
<problem>
<parameters>
<parameter name="3" value="3" />
</parameters>
</problem>

when i really want it to look like:
<?xml version="1.0" encoding="UTF-8"?>
<problem>
<parameters>
<parameter name="1" value="1" />
<parameter name="2" value="2" />
<parameter name="3" value="3" />
</parameters>
</problem>

My code is as follows, all help appreciated.


Best
Mark Hughes
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18141
    
    8

You only create one <parameter> element, that's why there is only one <parameter> element in your output. Just repeatedly changing the values of its attributes isn't going to magically create more <parameter> elements. If you want three <parameter> elements then create three of them.
Mark Hughes
Ranch Hand

Joined: Jul 14, 2006
Posts: 146
I have tried that, Creating more parameters seems to mess up the tree structure, and would not work great when trying to dynamically add new parameters when working with a for loop or another iterator function.

I was hoping some kind of function for adding attributes or children existed.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18141
    
    8

Mark Hughes wrote:I have tried that, Creating more parameters seems to mess up the tree structure, and would not work great when trying to dynamically add new parameters when working with a for loop or another iterator function.

If the commented-out line in your post is any indication, you've tried to add the same Element twice. Yes, that will mess things up. If you want three <parameter> Elements then create three of them. Don't create one and try to add it three times.
Mark Hughes wrote:I was hoping some kind of function for adding attributes or children existed.
You keep hoping for magic to happen, it seems. There's only programming, I'm afraid. If you want something to happen, just write code which makes it happen. (Or did I misunderstand that sentence? I got lost in the grammar near the end.)
Mark Hughes
Ranch Hand

Joined: Jul 14, 2006
Posts: 146
Hi Paul,

Cool, i just needed to understand how it worked, i have figured it out now based on what you suggested.



I will now be able to create XML that looks similar to what is defined in the dtd. All though i must say im still a bit unsure of how to use the DTD to confirm this programmatically, any suggestions?

Best
Mark Hughes
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18141
    
    8

Mark Hughes wrote:All though i must say im still a bit unsure of how to use the DTD to confirm this programmatically, any suggestions?


Like I said back at the beginning, generate your XML document from the DOM -- make sure to include a reference to your DTD, sorry I don't know how to do that in JDOM. Then run the XML document through a validating parser and see if any exceptions are thrown.
Mark Hughes
Ranch Hand

Joined: Jul 14, 2006
Posts: 146
ok no prob, thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using JDOM lib for parssing XML and writing out XML according to a DTD
 
Similar Threads
Allowing & br tags to show using XSL
How to transform an XML document to Java objects
getting exception please help
JDOM parsing and memory
How to resolve namespaces for XPath with JDOM