• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

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

 
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 28323
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 28323
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 28323
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 28323
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok no prob, thanks.
 
Please do not shoot the fish in this barrel. But you can shoot at this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic