0

I'm a newbie of XML programming, I'm trying to write some java objects to a well formed and valid XML file (with respect to a DTD file),

I found out that I can do this kind of things using JAXP with Java. My application is going to retrieve some data using an interface, and then I need to write that data into an XML file with respect of a DTD I previously and already created.

I tried to find some informations about this operations but I failed.

How am I supposed to do this operation?

EDIT: Please note that I need to stick with DTD (Can't switch to a XML schema) and that I need to go FROM Java Objects TO XML, and not viceversa. I found that the Duplicated Answer is not applying for my question.

Don't know if the DTD can be of any help, but here it is.

DTD

<!ELEMENT AIRCRAFTS (AIRCRAFT+)>
<!ELEMENT AIRCRAFT (MODEL, SEATS)>
<!ELEMENT MODEL (#PCDATA)>
<!ELEMENT SEATS (SEAT+)>
<!ELEMENT SEAT (#PCDATA)>

<!ELEMENT FLIGHTS (FLIGHTREADER+)>
<!ELEMENT FLIGHTREADER (DEPARTURE, TIME, DESTINATION)>
<!ELEMENT DEPARTURE (#PCDATA)>
<!ELEMENT TIME (HOUR, MINUTE)>
<!ELEMENT HOUR (#PCDATA)>
<!ELEMENT MINUTE (#PCDATA)>
<!ELEMENT DESTINATION (#PCDATA)>


<!ELEMENT FLIGHTINSTANCES (FLIGHTINSTANCEREADER+)>
<!ELEMENT FLIGHTINSTANCEREADER (AIRCRAFTID, DATE, DELAY, DEPARTUREGATE, FLIGHTREADERID, PASSENGERREADER+, STATUS)>
<!ELEMENT AIRCRAFTID (#PCDATA)>
<!ELEMENT DATE (#PCDATA)>
<!ELEMENT DELAY (#PCDATA)>
<!ELEMENT DEPARTUREGATE (#PCDATA)>
<!ELEMENT FLIGHTREADERID (#PCDATA)>
<!ELEMENT PASSENGERREADER (NAME, FLIGHTINSTANCEID, SEATID, BOARDED)>
<!ELEMENT FLIGHTINSTANCEID (#PCDATA)>
<!ELEMENT BOARDED (#PCDATA)>
<!ELEMENT NAME (#PCDATA)>
<!ELEMENT SEATID (#PCDATA)>
<!ELEMENT STATUS (#PCDATA)>

<!ATTLIST FLIGHTINSTANCEREADER id ID #REQUIRED>
<!ATTLIST FLIGHTREADER id ID #REQUIRED>
<!ATTLIST AIRCRAFT id ID #REQUIRED>
Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
Ivano
  • 91
  • 1
  • 11
  • This [answer](http://stackoverflow.com/questions/10312159/generate-java-classes-with-jaxb-from-a-dtd-file-how-can-i-modify-the-dtd#answer-14927126) answers your question. I should know, I tried this recently and it worked. – Buhake Sindi Jan 28 '15 at 17:02
  • Thank you for your answer, but I don't think the question is duplicated, the one you linked me is not answering my question and doesn't satisfy the constraints I have – Ivano Jan 28 '15 at 17:12
  • The answer link I provided allows you to generate Java Objects from DTD (using `xjc` which comes with the JDK in the `bin` folder). It will not create an XML schema but you can still use JAXB to generate XML from Java Object. Creating a schema from the DTD can be daunting. – Buhake Sindi Jan 28 '15 at 19:32
  • Firstly, thank you again for your time. What I need to do is to generate a valid XML (with respect to a DTD) starting from some java objects. I don't want to create Java Objects from a DTD, that's not my goal. I already have the Java objects, and I want to write them down to a valid XML file using JAXP. – Ivano Jan 29 '15 at 09:35

2 Answers2

1

It's basically the same way to convert Java Object to XML with JAXBContext and Marshaller with an addition of validation the XML validation with DTD.

See the sample code:

    SAXParserFactory spf = SAXParserFactory.newInstance();
    SAXParser sp = spf.newSAXParser();
    XMLReader xr = sp.getXMLReader();

    JAXBContext jc = JAXBContext.newInstance("blog.log4j");
    Unmarshaller unmarshaller = jc.createUnmarshaller();
    UnmarshallerHandler unmarshallerHandler = unmarshaller.getUnmarshallerHandler();
    xr.setContentHandler(unmarshallerHandler);

    FileInputStream xmlStream = new FileInputStream("src/blog/log4j/sample1.xml");
    InputSource xmlSource = new InputSource(xmlStream);
    xr.parse(xmlSource);

    Log4JConfiguration config = (Log4JConfiguration) unmarshallerHandler.getResult();

    Marshaller marshaller = jc.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    marshaller.marshal(config, System.out);

Source.

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
-1

First you need to serialize object to xml, then you need validate id against dtd.

Here you have example how to serialzie classes to xml.

This one shows how to validate xml file with dtd that is outside or inside xml file.

HuTa
  • 168
  • 1
  • 8
  • The question is about how to serialize the object in XML according to some DTD. This is not addressed in your answer. – lexicore Jan 29 '15 at 10:43