开发者

What does a basic DOM XML parser need?

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 X开发者_Go百科ML 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).


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜