It appears you have not yet registered with DEVPPL. To register please click here... (it's fast, easy and free!)

Forum

Log In Sponsors
Board index Programming JavaScript Forum

dynamically adding an SVG drawing to a page

dynamically adding an SVG drawing to a page

Postby matheson21 on Fri Oct 12, 2007 4:52 am

The task is to process using ajax technology an xml file which has svg content resulting in an image appearing on the page. I have not been able to do this and any solution would be interesting. The following can be saved in .xml file or a .svg file. When opened by Firefox or IE7 the .svg file displays a red square. The same occurs for the .xml file with Firefox but causes a known DTD parsing problem in IE7 (which is sort of incidental to me).
Here is the content of the files I refer to:
<?xml version="1.0" standalone="yes" ?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg xmlns="http://www.w3.org/2000/svg" width="400px" height="300px">
<desc>a single red square</desc>
<g id="oAll">
<rect id="oRect" x="50" y="100" width="100" height="100" fill="red"/>
</g>
</svg>

It turns out that Firefox working with standalone files does not need the XML declaration nor the DTD but certainly needs the namespace reference.

It is nice to see the SVG drawings operate in standalone files but what I need is to be able to fetch textual SVG data on the fly and display it on my page. By all appearances you are supposed to use the .innerHTML property to trigger the parsing of the XML (http://www.whatwg.org/specs/web-apps/current-work/#innerhtml1 section 2.5.3) but all I get is the descriptive alternative.

I have a simple .html and it appears below. Anyone who thinks they can help could save this text as a .html to demonstrate the problem. Here, when the file is loaded it firsly displays the unformatted xml data and then it runs the .innerHTML assignation. It then displays the descriptive alternative message instead of the red square.



Code: Select all
<html>
<head>
  <script>
function go(){
var xmlSVG='<?xml version="1.0" standalone="yes" ?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg xmlns="http://www.w3.org/2000/svg" width="400px" height="300px"><desc>a single red square</desc><g id="oAll"><rect id="oRect" x="50" y="100" width="100" height="100" fill="red"/></g></svg></xml>';
alert(xmlSVG);
var anElementObject=document.getElementById("anElement");
anElementObject.innerHTML=xmlSVG;
}
  </script>
</head>
<body onload="go()">
<div id="anElement">.....</div>
</body>
</html>

matheson21
 
Posts: 2
Joined: Fri Oct 12, 2007 3:44 am

Postby dafunkymunky on Sun Oct 14, 2007 4:52 pm

You just gave me a great project to work on..

K lemme tell you this for now... You cannot display a svg image..

Lemme tell you what XML is.. XML is a language of your own.. It doesn't have any specific inbuilt commands/tags as in html or other things... Here the tags are all your own.. You name the tag according to your convenience.. But the catch is you have to process the XML file yourself.. Means to say you have to program what to do with the data in the file.. Your program has to interpret the data that is in the file. and decide what to do with it. Firefox recognises XML means that it can identify various tags in the file and separate them(there are functions to separate them) But as i said before, it wont know what to do with it. You have to do whatever you want to do with it. Firefox only helps in separating the data for you. An external svg viewer also identifies the code and there it has the necessary functions to perform various drawing operations. Say it'll identify <rect id="oRect" x="50" y="100" width="100" height="100" fill="red"/> and it'll have separate function to draw the rectange and gill it with the specified color..

You'll have to write such functions if you want to view the file as you say..
--------DAFUNKYMUNKY--------
::THE::NECESSARY::[D]EVIL::
....errr i mean munky....
User avatar
dafunkymunky
100+ Club
 
Posts: 183
Joined: Fri Apr 08, 2005 7:32 am
Location: India

Postby matheson21 on Sun Oct 14, 2007 9:15 pm

To DAFUNKYMUNKY:
Thanks for your comprehensive reply. I appreciate your help.

I have used a fair bit of XML in exactly the customized way that you describe but assumed the graphical output for SVG content is taken care of by the browser - after all the browser properly renders the red square when Firefox opens the .Xml file.

Many thanks, I am going to have to study this further it seems.
matheson21
 
Posts: 2
Joined: Fri Oct 12, 2007 3:44 am


Who is online

Users browsing this forum: No registered users and 11 guests