tags:

views:

56

answers:

2

I've started programming in Google's Go Language, and the package I'm attempting to write is an API for processing and creating DOCX files (I'm familiar with this topic and thought it would be a good way to learn Go). As DOCX files are primarly a ZIP file with various XML files inside them, I rather need a DOM XML parser. However, I was unable to find any native Go DOM XML Parsers, as the only ones I saw seemed to be very limited, and probably SAX parsers (anyone who uses Go, correct me if I'm wrong).

So this past weekend I wrote a very basic DOM XML parser that was able to parse one of the simpler XML files within the DOCX package and output it back intact. At the moment I'm not going to bother with Namespace, XSLT, or schema validation support, as those aren't useful for manipulating DOCX files. My question is, what other XML standards and functionality would be important to incorporate into the parser?

At the moment, it only really just creates a tree of elements and attributes, which I can modify and save. I'm not current handling CDATA elements or XML escape characters (though those would be easy to do and I'll get to that this weekend).

+3  A: 

First of all: if you specifically want to do DOM parser, you need to implement DOM API. But I am not sure if you actually mean that; perhaps you just mean an XML parser that produces XML tree model ("dom"); or just an XML parser? DOM is hardly the only way. Also note that implementing DOM tree model using SAX parser is the most common way; few if any DOM packages have embedded parsers, commonly parser is exposed separately.

As to XML parser features, some of things that are MUSTs in my opinion are:

  • Handling of character entities (ampersand and number), pre-defined general entities (lt, gt, apos, quot)
  • Handling of xml declaration ()
  • Handling of various input encodings; declared by xml declaration or externally -- too many parsers skimp on this, but is very imporant since xml documents can reliably detect encoding internally.
  • Checking for uniqueness of attribute values
  • Checking for proper nesting of elements
  • Skipping of comments
  • Skippping (if not handling) of processing instructions
  • CDATA handling -- it's simple to do
  • Keeping track of line numbers for error reporting

Other eventually useful things are:

  • Namespace handling
  • Checking of character validity, both content and names
  • Normalization of lineefeds as per xml specification
StaxMan
Yeah, precise terminology has never been my strong suit. A XML Tree model API is what I am aiming for.
Matt G
Yeah no sweat.. it's pretty common to call any tree model dom anyway. :)
StaxMan
A: 

Have you looked at Go's XML parser? http://golang.org/pkg/xml/

If it is missing functionality you need, it's probably still easier to add than roll your own.

cthom06
Yeah, thats one of the two packages I saw that already existed. However, it didn't seem to expose the api like I wanted. (Tree-model as I've concluded above). I also saw there is another third-party XML package listed as well, but that seemed to be in the same boat. (Do let me know if I'm interpreting these packages wrong)
Matt G
@Matt ahh, I didn't do my homework. I had assumed the xml package worked like the JSON package, which can unmarshal into an empty interface what is basically a tree structure of the next element.
cthom06