List Info

Thread: Loading from file to SVGDocument and ignoring whitespace?




Loading from file to SVGDocument and ignoring whitespace?
country flaguser name
United States
2007-07-25 11:41:14
I use the following to load a file to an SVGDocument:

private static String className =
        XMLResourceDescriptor.getXMLParserClassName();
private static SAXSVGDocumentFactory factory =
        new SAXSVGDocumentFactory(className);
...
factory.createSVGDocument(svgFile.toURI().toString());

The problem is, when I walk the DOM tree to discover
information about
each Node and its siblings, I end up with a bunch of empty
whitespace
nodes.  When I build an SVGDocument in memory, I don't have
this
problem.  How can I load an SVGDocument to a File and have
it ignore
whitespace?

<rect .../>
*** LOADING FROM A FILE REPORTS WHITESPACE HERE
<circle .../>

I want the Node representing the <circle> element's
previous sibling to
be the <rect>:

Node rectNode = circleNode.getPreviousSibling();

Michael Bishop

------------------------------------------------------------
---------
To unsubscribe, e-mail: batik-users-unsubscribexmlgraphics.apache.org
For additional commands, e-mail: batik-users-helpxmlgraphics.apache.org


Re: Loading from file to SVGDocument and ignoring whitespace?
country flaguser name
Australia
2007-07-25 20:29:36
Hi Michael.

Bishop, Michael W. CONTR J9C880:
> I use the following to load a file to an SVGDocument:
> 
> private static String className =
>         XMLResourceDescriptor.getXMLParserClassName();
> private static SAXSVGDocumentFactory factory =
>         new SAXSVGDocumentFactory(className);
> ...
> factory.createSVGDocument(svgFile.toURI().toString());
> 
> The problem is, when I walk the DOM tree to discover
information about
> each Node and its siblings, I end up with a bunch of
empty whitespace
> nodes.  When I build an SVGDocument in memory, I don't
have this
> problem.  How can I load an SVGDocument to a File and
have it ignore
> whitespace?

I don’t think there’s a way to tell the SAX parser to
ignore such
whitespace nodes.  You can, of course, write a
post-processing step to
remove all those text nodes from the document, e.g.:

  boolean isWhitespace(Node n) {
      if (n.getNodeType() != Node.TEXT_NODE) {
          return false;
      }
      String s = n.getNodeValue();
      for (int i = 0; i < s.length(); i++) {
          char c = s.charAt(i)
	  if (c != 'n' && c != 'r' && c != 't'
&& c != ' ') {
	      return false;
	  }
      }
      return true;
  }

  void removeWhitespaceNodes(Node n) {
      while (n != null) {
          Node next = n.getNextSibling();
	  if (isWhitespace(n)) {
	      n.getParentNode().removeChild(n);
	  } else {
	      Node child = n.getFirstChild();
	      if (child != null) {
	          removeWhitespaceNodes(child);
	      }
	  }
	  n = next;
      }
  }

(completely untested)

then call removeWhitespaceNodes on the document element.

-- 
Cameron McCormack, http://mcc.id.au/
	xmpp:heycamjabber.org  ▪  ICQ 26955922  ▪  MSN cammcc.id.au

------------------------------------------------------------
---------
To unsubscribe, e-mail: batik-users-unsubscribexmlgraphics.apache.org
For additional commands, e-mail: batik-users-helpxmlgraphics.apache.org


Re: Loading from file to SVGDocument and ignoring whitespace?
user name
2007-07-25 12:16:58
Hi,

On Wed, 2007-07-25 at 12:41 -0400, Bishop, Michael W. CONTR
J9C880
wrote:
> I use the following to load a file to an SVGDocument:
> 
> private static String className =
>         XMLResourceDescriptor.getXMLParserClassName();
> private static SAXSVGDocumentFactory factory =
>         new SAXSVGDocumentFactory(className);
> ...
> factory.createSVGDocument(svgFile.toURI().toString());
> 
> The problem is, when I walk the DOM tree to discover
information about
> each Node and its siblings, I end up with a bunch of
empty whitespace
> nodes.  When I build an SVGDocument in memory, I don't
have this
> problem.  How can I load an SVGDocument to a File and
have it ignore
> whitespace?

IMHO, any conforming svg parser should have a whitespace
node in the DOM
if the document is structured like your example. We
experience this
problem as well, so we do the workaround by put node filter
(that check
for whitespace outside <text> node with xml space
preserve) every time
we traverse the DOM Tree.

Another note: when you save the document into file, don't do
pretty
formating or you will get this whitespace problem. Or if you
really want
to pretty format the xml source, break at attribute level,
do not break
at element level, eg: <rect ...><circle ..break
here at attribute level
so the >< (end tag and open tag) is side by side

Regards
Tonny Kohar
-- 
Kiyut
imagine, design, create ...
http://www.kiyut.com


------------------------------------------------------------
---------
To unsubscribe, e-mail: batik-users-unsubscribexmlgraphics.apache.org
For additional commands, e-mail: batik-users-helpxmlgraphics.apache.org


Re: Loading from file to SVGDocument and ignoring whitespace?
country flaguser name
United States
2007-07-26 05:05:59
Cameron, how can that be? Shouldn't a dom tree be a dom
tree, pretty printed
or not?

Or did you loose me somewhere and am I just getting it
wrong...

Vincent


-- 
View this message in context: http://w
ww.nabble.com/Loading-from-file-to-SVGDocument-and-ignoring-
whitespace--tf4143370.html#a11807660
Sent from the Batik - Users mailing list archive at
Nabble.com.


------------------------------------------------------------
---------
To unsubscribe, e-mail: batik-users-unsubscribexmlgraphics.apache.org
For additional commands, e-mail: batik-users-helpxmlgraphics.apache.org


Re: Loading from file to SVGDocument and ignoring whitespace?
country flaguser name
United States
2007-07-26 05:33:07
Ok, strange however... XML should be formatting
independent.

I guess this gives (only) a memory penalty to Batik ?

Vincent  
-- 
View this message in context: http://w
ww.nabble.com/Loading-from-file-to-SVGDocument-and-ignoring-
whitespace--tf4143370.html#a11807974
Sent from the Batik - Users mailing list archive at
Nabble.com.


------------------------------------------------------------
---------
To unsubscribe, e-mail: batik-users-unsubscribexmlgraphics.apache.org
For additional commands, e-mail: batik-users-helpxmlgraphics.apache.org


Re: Loading from file to SVGDocument and ignoring whitespace?
user name
2007-07-26 16:36:25
The text surrounding XML elements is part of the XML... and therefore part of the DOM... even if it's only "whitespace";.

On 7/26/07, V. de Weger < batikbeno-delft.nl">batikbeno-delft.nl> wrote:
Ok, strange however... XML should be formatting independent.


__________________________________________________________________________
Archie Cobbs ; &nbsp; &nbsp; *&nbsp;   ; &nbsp; &nbsp;CTO, Awarix&nbsp; &nbsp; &nbsp; &nbsp; * &nbsp; &nbsp; &nbsp; http://www.awarix.com
Re: Loading from file to SVGDocument and ignoring whitespace?
country flaguser name
United States
2007-07-27 03:56:20
Starting from <svg> ... </svg> it seemed
strange, however from <text> ..
</text> it perfectly clear. 

Still learning every day...

Vincent
-- 
View this message in context: http://w
ww.nabble.com/Loading-from-file-to-SVGDocument-and-ignoring-
whitespace--tf4143370.html#a11825284
Sent from the Batik - Users mailing list archive at
Nabble.com.


------------------------------------------------------------
---------
To unsubscribe, e-mail: batik-users-unsubscribexmlgraphics.apache.org
For additional commands, e-mail: batik-users-helpxmlgraphics.apache.org


[1-7]

about | contact  Other archives ( Real Estate discussion Medical topics )