Custom Java XMLBuilder vs. Standard Classes

What is the best solution for generating XML.

My goal is to create some simple XML files from code. I am going to implement a simple custom implementation of XML Builder based on StringBuffer. On the other hand, there are several libraries like http://code.google.com/p/java-xmlbuilder/ and http://code.google.com/p/xmltool/ which has a good DSL but I don’t confident in performance.

Since my goal is to build a fairly simple XMLBuilder with great performance, I think I’ll create my own solution. It will show:

  • Good Java-DSL for XML constructs (tagging mostly)
  • Great performance based on StringBuffer.
  • Outlier control of String data when adding XML tags.
  • Indenting

Please suggest if I am mistaken about performance expectations and it might be better to use ready-made libraries.

UPDATE. Why I think that the performance of standard xml developers is not very good.

Standard XML builders use Document Builder Factory and work with classes behind the scenes. Also, these classes are optimized for all users. For example, I do not need namespace support, etc.

<?xml version="1.0" encoding="utf-8">
<root>
 <testdata>value</testdata>
</root>
</xml>

Consider the very simple XML code above. If you create with standard tools, it will require a lot of work to make this simple XML. I find it better to just create it yourself using String.

2. , , XML .

3. ! , , ​​ "". - DSL XML XML.

Java DSL XML XML, XStream, .

4. JAXB. XStream vs JAXB , JAXB , XStream. JAXB , . JAXB, XStream , JAXB , .

+5
4

- , -...

.

, , - , , . , , , , /.

. , , . , .

+4

:

XML- Document Builder Factory . . .

DOM StAX (JSR-173). API XML, . , Woodstox, .

+3

Groovy NodeBuilder (http://groovy.codehaus.org/GroovyMarkup).

def root = new NodeBuilder()
  .people(kind:'folks', groovy:true) {
    person(x:123,  name:'James', cheese:'edam') {
      project(name:'groovy')
      project(name:'geronimo')
    }
    person(x:234,  name:'bob', cheese:'cheddar') {
      project(name:'groovy')
      project(name:'drools')
    }
  }
XmlUtil.serialize(root, System.out)

XML-:

<?xml version="1.0" encoding="UTF-8"?>
<people kind="folks" groovy="true">
  <person x="123" name="James" cheese="edam">
    <project name="groovy"/>
    <project name="geronimo"/>
  </person>
  <person x="234" name="bob" cheese="cheddar">
    <project name="groovy"/>
    <project name="drools"/>
  </person>
</people>
+2

: StaxMate - , Stax XML-, (40 - 80 , ). , JDK 6 Stax (Sun sjsxp), - , , Woodstox Aalto.

XML; , , ( , - ), , , , ( ; , ). ... , - ?

But if you want to do something higher and higher than existing writers, you might consider using a simple script and complementing it with the additional functionality you need. For example, if you just use Stax XMLStreamWriter as a base, it is quite simple to add simple but effective abstractions. Or, if you like existing packages, see if you can offer improvements for your authors (or even code contributions).

+1
source

All Articles