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 Pae Choi <pa...@earthlink.net> on 2002/10/20 20:31:51 UTC

DOM with no indentation to DOM with indentation

Say we have generated a DOM as follows:

<Parent><Child><Name>John
Doe</Name><Gender>Male</Gender></Child>><Child><Name>John
Doe</Name><Gender>Male</Gender></Child></Parent>

As you can there is Node.TEXT_NODE for indentation such as "\n", "\t", or so
other than values for <Name> and <Gender> elements.

My question is that if there is any utility available to convert the
above DOM to "the following DOM with indentation" as follows:

<Parent>
    <Child>
        <Name>John Doe</Name>
        <Gender>Male</Gender>
    </Child>
    <Child>
        <Name>Jane Doe</Name>
        <Gender>Female</Gender>
    </Child>
</Parent>

If there is a utility class something like:

import    org.w3c.dom.Document;

public class XMLUtil {

    /** Convert the non-indented DOM to an indented DOM */
    public Document covnertNonIndentedDOM2IndentedDOM(Document inboundXML) {
        Document outboundXML = null;

        // snip

        return outboundXML;
    }
}

will be nice to know. So it can be done by:

XMLUtil xmlUtil = new XMLUtil();
Document respXML = xmlUtil.covnertNonIndentedDOM2IndentedDOM(reqXML);

Any info on this subject are welcome and will be appreciated. Thank you.

Regards,


Pae



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


RE: DOM with no indentation to DOM with indentation

Posted by Aleksandar Milanovic <am...@galdosinc.com>.
How about you parse the document to a DOM structure and then serialize it
with indentation? For serialization, use the
org.apache.xml.serialize.OutputFormat and
org.apache.xml.serialize.XMLSerializer classes from the Xerces package.
In OutputFormat, use setIndenting() and setIndent() methods to set the
indentation to the desired level.

Alex

-----Original Message-----
From: Pae Choi [mailto:paechoi@earthlink.net]
Sent: October 20, 2002 3:44 PM
To: xerces-j-user@xml.apache.org; xerces-j-dev@xml.apache.org
Subject: Re: DOM with no indentation to DOM with indentation


Oops, I did not consider the line wrapping. And the initial DOM sample
in the previous message contains typos. The correct DOM sample should
be as follows:

<Parent><Child><Name>John
Doe</Name><Gender>Male</Gender></Child>><Child><Name>Jane
Doe</Name><Gender>Feale</Gender></Child></Parent>

*** Note: If above sample does not show in a single line, it's probably
cause by line wrapping. Then please consider that as a single line.
Thanks again.

Regards,


Pae




> Say we have generated a DOM as follows:
>
> <Parent><Child><Name>John
> Doe</Name><Gender>Male</Gender></Child>><Child><Name>John
> Doe</Name><Gender>Male</Gender></Child></Parent>
>
> As you can there is Node.TEXT_NODE for indentation such as "\n", "\t", or
so
> other than values for <Name> and <Gender> elements.
>
> My question is that if there is any utility available to convert the
> above DOM to "the following DOM with indentation" as follows:
>
> <Parent>
>     <Child>
>         <Name>John Doe</Name>
>         <Gender>Male</Gender>
>     </Child>
>     <Child>
>         <Name>Jane Doe</Name>
>         <Gender>Female</Gender>
>     </Child>
> </Parent>
>
> If there is a utility class something like:
>
> import    org.w3c.dom.Document;
>
> public class XMLUtil {
>
>     /** Convert the non-indented DOM to an indented DOM */
>     public Document covnertNonIndentedDOM2IndentedDOM(Document inboundXML)
{
>         Document outboundXML = null;
>
>         // snip
>
>         return outboundXML;
>     }
> }
>
> will be nice to know. So it can be done by:
>
> XMLUtil xmlUtil = new XMLUtil();
> Document respXML = xmlUtil.covnertNonIndentedDOM2IndentedDOM(reqXML);
>
> Any info on this subject are welcome and will be appreciated. Thank you.
>
> Regards,
>
>
> Pae
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
> For additional commands, e-mail: xerces-j-user-help@xml.apache.org
>


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





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


Re: DOM with no indentation to DOM with indentation

Posted by Pae Choi <pa...@earthlink.net>.
Oops, I did not consider the line wrapping. And the initial DOM sample
in the previous message contains typos. The correct DOM sample should
be as follows:

<Parent><Child><Name>John
Doe</Name><Gender>Male</Gender></Child>><Child><Name>Jane
Doe</Name><Gender>Feale</Gender></Child></Parent>

*** Note: If above sample does not show in a single line, it's probably
cause by line wrapping. Then please consider that as a single line.
Thanks again.

Regards,


Pae




> Say we have generated a DOM as follows:
>
> <Parent><Child><Name>John
> Doe</Name><Gender>Male</Gender></Child>><Child><Name>John
> Doe</Name><Gender>Male</Gender></Child></Parent>
>
> As you can there is Node.TEXT_NODE for indentation such as "\n", "\t", or
so
> other than values for <Name> and <Gender> elements.
>
> My question is that if there is any utility available to convert the
> above DOM to "the following DOM with indentation" as follows:
>
> <Parent>
>     <Child>
>         <Name>John Doe</Name>
>         <Gender>Male</Gender>
>     </Child>
>     <Child>
>         <Name>Jane Doe</Name>
>         <Gender>Female</Gender>
>     </Child>
> </Parent>
>
> If there is a utility class something like:
>
> import    org.w3c.dom.Document;
>
> public class XMLUtil {
>
>     /** Convert the non-indented DOM to an indented DOM */
>     public Document covnertNonIndentedDOM2IndentedDOM(Document inboundXML)
{
>         Document outboundXML = null;
>
>         // snip
>
>         return outboundXML;
>     }
> }
>
> will be nice to know. So it can be done by:
>
> XMLUtil xmlUtil = new XMLUtil();
> Document respXML = xmlUtil.covnertNonIndentedDOM2IndentedDOM(reqXML);
>
> Any info on this subject are welcome and will be appreciated. Thank you.
>
> Regards,
>
>
> Pae
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
> For additional commands, e-mail: xerces-j-user-help@xml.apache.org
>


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


Re: DOM with no indentation to DOM with indentation

Posted by Pae Choi <pa...@earthlink.net>.
Oops, I did not consider the line wrapping. And the initial DOM sample
in the previous message contains typos. The correct DOM sample should
be as follows:

<Parent><Child><Name>John
Doe</Name><Gender>Male</Gender></Child>><Child><Name>Jane
Doe</Name><Gender>Feale</Gender></Child></Parent>

*** Note: If above sample does not show in a single line, it's probably
cause by line wrapping. Then please consider that as a single line.
Thanks again.

Regards,


Pae




> Say we have generated a DOM as follows:
>
> <Parent><Child><Name>John
> Doe</Name><Gender>Male</Gender></Child>><Child><Name>John
> Doe</Name><Gender>Male</Gender></Child></Parent>
>
> As you can there is Node.TEXT_NODE for indentation such as "\n", "\t", or
so
> other than values for <Name> and <Gender> elements.
>
> My question is that if there is any utility available to convert the
> above DOM to "the following DOM with indentation" as follows:
>
> <Parent>
>     <Child>
>         <Name>John Doe</Name>
>         <Gender>Male</Gender>
>     </Child>
>     <Child>
>         <Name>Jane Doe</Name>
>         <Gender>Female</Gender>
>     </Child>
> </Parent>
>
> If there is a utility class something like:
>
> import    org.w3c.dom.Document;
>
> public class XMLUtil {
>
>     /** Convert the non-indented DOM to an indented DOM */
>     public Document covnertNonIndentedDOM2IndentedDOM(Document inboundXML)
{
>         Document outboundXML = null;
>
>         // snip
>
>         return outboundXML;
>     }
> }
>
> will be nice to know. So it can be done by:
>
> XMLUtil xmlUtil = new XMLUtil();
> Document respXML = xmlUtil.covnertNonIndentedDOM2IndentedDOM(reqXML);
>
> Any info on this subject are welcome and will be appreciated. Thank you.
>
> Regards,
>
>
> Pae
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
> For additional commands, e-mail: xerces-j-user-help@xml.apache.org
>


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


Re: DOM with no indentation to DOM with indentation

Posted by Joseph Kesselman <ke...@us.ibm.com>.
Unless you are extremely careful about how yout do it (only inserting 
indentation in places where whitespace is not meaningful, as defined by 
the DTD and/or Schema and/or your program's understanding of the data), 
indentation can change the meaning of your document. That's why it isn't 
the default.

I'm not sure whether Xerces' serializers offer an indentation option. 

XSLT does, via the  <xml:output indent="yes"/> setting, so you could use 
Xalan and an identity transformation to get this result. (You'd also have 
to set the indentation amount; I believe Xalan currently defaults to 0 -- 
but the Xalan lists would be the right place for to go into detail on 
that.)
______________________________________
Joe Kesselman  / IBM Research

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


Re: DOM with no indentation to DOM with indentation

Posted by Joseph Kesselman <ke...@us.ibm.com>.
Unless you are extremely careful about how yout do it (only inserting 
indentation in places where whitespace is not meaningful, as defined by 
the DTD and/or Schema and/or your program's understanding of the data), 
indentation can change the meaning of your document. That's why it isn't 
the default.

I'm not sure whether Xerces' serializers offer an indentation option. 

XSLT does, via the  <xml:output indent="yes"/> setting, so you could use 
Xalan and an identity transformation to get this result. (You'd also have 
to set the indentation amount; I believe Xalan currently defaults to 0 -- 
but the Xalan lists would be the right place for to go into detail on 
that.)
______________________________________
Joe Kesselman  / IBM Research

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