You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xerces.apache.org by Henrik Johansson <c0...@cs.umu.se> on 2003/12/11 00:20:34 UTC

preserve source structure

Is it possible to preserve the structure from an input file when
serializing its DOM-tree to an output file?

Example:
---
   <book> <title>
           The Clue
          </title>
          <chapters>
           13
          </chapters>
   </book>
---

1. Parse XML-document.                 -> DOM-tree
2. Change a node value in the DOM-tree -> DOM-tree'
3. Serialize DOM-tree'

---
   <book> <title>
           The Clue
          </title>
          <chapters>
           20
          </chapters>
   </book>
---

Thanks! (and yes, I know the indentation in the example sucks... ;)

---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-user-help@xml.apache.org


Re: preserve source structure

Posted by Simon Kitching <si...@ecnetwork.co.nz>.
On Thu, 2003-12-11 at 12:20, Henrik Johansson wrote:
> Is it possible to preserve the structure from an input file when
> serializing its DOM-tree to an output file?
> 
> Example:
> ---
>    <book> <title>
>            The Clue
>           </title>
>           <chapters>
>            13
>           </chapters>
>    </book>

Well, the "book" element has the following children:
* a text node containing one space
* a title element
* a text node containing a linefeed and about 10 spaces
* a chapter element
* a text node containing a linefeed and about 5 spaces.

The title element has one child, being a text node containing a
linefeed, about 10 spaces, "The Clue", a linefeed and another 10 spaces.

etc.

So provided you do *not* have a schema which specifies that <book> is of
"element content only", and you serialize without "pretty-printing", I
would expect that you would get back something pretty similar when you
serialize the dom back out again.

Note however that, in general, xml parsers don't preserve input
formatting. Most xml parsers (including xerces) provide no mechanism for
preserving xml attribute order for example, because attribute order is
not meaningful in xml.

There are some posts on this subject in the email archives, particularly
from people trying to write xml/xslt editing programs. I don't know what
solution (if any) they came up with to this issue.

Regards,

Simon



---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-user-help@xml.apache.org