Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

From time to time I create whole documents (e.g. XML or proprietary) programmatically.

If it easy enough, I just write something like

stringbuilder.append("<blah>");
stringbuilder.append("<blubb>");
stringbuilder.append(someValue);
stringbuilder.append("</blubb>");
stringbuilder.append("</blah>");

But what if the document schema gets more complex? How would one describe it? What if it is not well structured like XML, but has an arbitrary format?

Right now I thought about an abstract description of the format that just needs to get the blanks filled. What if I also have to parse such a document?

But this seems to be a recurring problem to me.

So: Is there some "pattern" to solve that?

share|improve this question

closed as unclear what you're asking by Jim G., gnat, mattnz, Dan Pichelman, Kilian Foth Jul 14 at 10:20

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question.If this question can be reworded to fit the rules in the help center, please edit the question.

    
One pattern is to use an object and then serialize/deserialize that object (to XML, JSON or whatever format you're using). When you want to save/load it. –  Benjamin Gruenbaum Jul 11 '13 at 19:37
    
Thats the reason for the serialisation tag. I think there could be a solution that is easier for more complicated formats, e.g. SVG (just to have a really big one) –  Mare Infinitus Jul 11 '13 at 19:41

2 Answers 2

For well known file formats, there is often a library that will write out that file format for you.

For example, with XML, you can use jaxb - there is a reasonable tutorial for this at oracle. You would have the appropriate schema and let jaxb create the java classes for you. From this, you would then set the appropriate fields in the objects and marshal it, and you've got the XML.

Other file formats would follow a similar approach. You create an object that has all the data that you are after in java, and then write the appropriate serilization for it to a file. This is done by implmenting Serializable and the associated methods of readObject and writeObject.


Similar approaches can be taken in other languages. The idea is basically the same - create a data object for the file format and find (or write) the associated serialization.

You will tend to find it easy to write things to serlize to and from xml (and JSON). For example, in perl one can use XML::Dumper to do the work for you.

If it isn't a standard format you are working with, you will need to write your own methods to seralize and deserialize data.

Look at Serialization in wikipedia under "Programming language support" to see the tools in different languages - it is mostly just a matter of finding the right concept within the language and its associated methods.

share|improve this answer
    
Writing a big DTO object graph is an option. Proving a schema and get the classes generated sounds interesting though. Can you provide more information about that please? Probably that ends in a DSL, or? –  Mare Infinitus Jul 11 '13 at 19:46
    
@MareInfinitus jaxb samples has an example, step by step walkthrough that goes from an xsd, through "compiling" it to a set of .java files, the associated factories, and then creating an xml file from the generated classes. –  MichaelT Jul 11 '13 at 19:54
    
But what about non-XML formats? What about other languages than Java? –  Mare Infinitus Jul 11 '13 at 19:57
    
@MareInfinitus I've updated the answer - the key is to the serialization approaches for the appropriate language, and if its a widely used format, you will likely find existing libraries to do the heavy lifting for you. –  MichaelT Jul 11 '13 at 20:07
    
@kevincline it depends on the complexity of the object and the degree of certainty that you want when creating the XML. The possibility of messed up XML (XML insertion attacks), unescaped strings, etc. Or simple ones, sure... But complex ones it can be difficult at times. –  MichaelT Jul 11 '13 at 22:48

The answer depends a lot on the thing you are trying to create. For hierarchical structures like XML and HTML I would strongly consider Groovy Builders or similar facilities in other dynamic languages. Using Groovy Builders, the structure of the code matches the structure of the file you are trying to create:

html {
 body {
  h1 title
  ...
 }

}

Since it's just code, you have the full power of the programming language available:

 ul {
   items.each { item ->
     li "Item ${item}"
   }
 }

For formats like JSON and YAML which map directly to arrays and maps, it would make sense to build the data structure and serialize it. You might also want to look at macro facilities like Velocity, which makes it easy to modify and test document templates without any need to recompile code.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.