Search code examples
javaxmlxsddocbook

How to generate Docbook xml from Java bindings/API?


I’d like to be able to generate Docbook xml files programatically by calling the Java API to build the DOM and then emitting it to a properly formatted file. Unfortunately I’ve not been able to find any examples of using the Java binding in this way so I’m stuck.

Someone has helpfully generated the Java binding from the 5.x Docbook XSD and parked it here.

Wikipedia has a nice short HelloWorld-type example of what I'd like to generate but I can't seem to figure out which methods to call when:

 <?xml version="1.0" encoding="UTF-8"?>
  <book xml:id="simple_book" xmlns="http://docbook.org/ns/docbook" version="5.0">
    <title>Very simple book</title>
    <chapter xml:id="chapter_1">
      <title>Chapter 1</title>
      <para>Hello world!</para>
      <para>I hope that your day is proceeding <emphasis>splendidly</emphasis>!</para>
    </chapter>
    <chapter xml:id="chapter_2">
      <title>Chapter 2</title>
      <para>Hello again, world!</para>
    </chapter>
  </book>

Or, failing that, how could I parse the example above into the Java binding?


Solution

  • From my perspective it is not possible to properly bind such complex schema as DocBook. There are almost unlimited ways how elements can be combined and moreover, DocBook utilizes mixed content, which brings another level of complexity, see https://blogs.oracle.com/mgrebac/entry/handling_extended_mixed_content_in

    If your source has a fixed structure, the final XML can be generated by various ways. I'd start with StAX https://docs.oracle.com/javase/tutorial/jaxp/stax/why.html