Hi,

How about trying this ways
1) String s = <g id="101" ....> </g> surround it with svg tag
eg:
<svg xmlns="..">your <g> element </svg>

2) parse it using batik svg parser not the default Java DocumentBuilder
parser, Batik svg parser is namespace aware.

3) append as usual, but keep in mind that the <g> element is one layer
deeper than previous because of the surrounding <svg> tag

Regards
Tonny Kohar

On Tue, 2007-04-24 at 04:50 -0400, [EMAIL PROTECTED] wrote:
> Hello,
> 
> I try to add new SVG content to a SVG Document which already exists.
> 
> 1) I load a svg file with the jsvgcanvas class
> 
> svgCanvas.setDocumentState(JSVGCanvas.ALWAYS_DYNAMIC);
> svgCanvas.getActionMap().get(JSVGCanvas.RESET_TRANSFORM_ACTION).actionPerformed(ae);
> svgCanvas.setURI(url);
> 
> 2) I get the SVGDocument object which contains DOM tree of the SVG
> 
> document = svgCanvas.getSVGDocument();
> 
> 3) I get a String (=new svg content) from a Servlet
> 
> String s = 
> "<g id="101" name="test" xmlns="http://www.w3.org/2000/svg";>
> <polyline points="8325 2232, 8325 2540" style="fill:none; 
> stroke:rgb(255,000,000);"/>
> <polyline points="7925 2232, 7925 2540" style="fill:none; 
> stroke:rgb(255,000,000);"/>
> <polyline points="7525 2232, 7525 2540" style="fill:none; 
> stroke:rgb(255,000,000);"/>
> ...
> </g>"
> 
> 4) I parse it into a new Document object
> 
> InputSource is = new InputSource(new StringReader(s));
> 
> DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
> DocumentBuilder db = dbf.newDocumentBuilder();
> 
> Document newLayer = db.parse(is);
> 
> 5) I add the root element of the new document in the main document
> 
> Element root = (Element)document.getDocumentElement();
> Node a1 = document.getElementById("1");
> 
> Element newLayerRoot = (Element)newLayer.getDocumentElement();
> 
> //add the new Layer to the main document
> a1.appendChild(document.importNode(b1, true));
> 
> svgCanvas.setSVGDocument((SVGDocument)document);
> 
> -------------------------------------------------------------------------------------------
> 
> The problem is that the JSVGCanvas doesn't refresh!
> 
> However:
> -When I serialize the main document after the add of the new content, The 
> final document is ok!
> If I open load it with the svgcanvas.seturi(), It show me the good picture 
> with the new content.
> BUT: There is smth special with the namespace of the g element from the 
> newLayer document. 
> There is no namespace for this element in the file
> <g name="it is the good string" id="101">
> 
> -svgCanvas is svgCanvas.setDocumentState(JSVGCanvas.ALWAYS_DYNAMIC);
> 
> 
> -if I try to add a simple rect, it works!: 
> String svgNS=" http://www.w3.org/2000/svg";;
> Element groupe = document.createElementNS(svgNS, "g");
> Element rectangle = document.createElementNS (svgNS, "rect");
> rectangle.setAttributeNS(null, "x", "1200");
> rectangle.setAttributeNS(null, "y", "2200" );
> rectangle.setAttributeNS(null, "width", "3100");
> rectangle.setAttributeNS(null, "height", "3510");
> rectangle.setAttributeNS(null, "style", "stroke: red; fill: red;");
> groupe.appendChild(rectangle);
> a1.appendChild(rectangle);
> 
> But:
> I must add the new element from a String! so I can't create new ElementNS.
> I try to add the namespace to the String
> "<g id="101" name="test" xmlns="http://www.w3.org/2000/svg";>..."
> 
> But smth is strange with the namespace:
> Element b1 = (Element)newLayer.getDocumentElement(); //b1 is the g element 
> from the string
> 
> System.out.println("Element:" + b1.getTagName() + " Name:" + 
> b1.getAttribute("name") + " Namespace:" + b1.getNamespaceURI());
> //gives me: "Element:g Name:test Namespace:null"
> 
> ---------------------------------------------------------------------------------------------
> 
> Why getNamespaceURI() gives me "null"? Why when I serialize the document it 
> doesen't show me any namespace for this element?
> How can refresh a JSVGCanvas when I add a new String with a namespace (which 
> disapear during the parsing...) to a SVGDocument which already exists?
> 
> Help!
> 
> Ps: sorry for my english ;-)
> 
> 
> 
> 
> 
> --
> This message was sent on behalf of [EMAIL PROTECTED] at openSubscriber.com
> http://www.opensubscriber.com/messages/[EMAIL PROTECTED]/topic.html
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> 
-- 
Sketsa 
SVG Graphics Editor
http://www.kiyut.com


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to