getURL() and parseXML() to load an XML file

paxiinieintaxi
05-01-2004, 04:49 AM
hi.

i'm using the following Javascript-code to import a XML-file into my SVG-application:

...
getURL("./graph_xml_example.xml", my_callback);
...

function my_callback( urlRequestStatus ) {

if (urlRequestStatus.success)
{
alert('loaded stuff');
alert(urlRequestStatus.content);

xmlDoc = parseXML(urlRequestStatus.content, null);
// null -> create a new XML doc
alert(xmlDoc);

var xmlRoot = xmlDoc.getDocumentElement();
alert(xmlRoot);

var node3 = xmlDoc.getElementById("masterblaster");
alert(node3);

var label3 = node3.getAttribute("label");
alert(label3);
}
else
{
alert('nuttin\' loaded');
}
}

loading the XML-file works fine so far, but I can't work with the XML-file.

alert(xmlDoc) gives: [object Document]
that's ok, I suppose :P

alert(xmlRoot) gives: [object Element]
seems to be ok as well, but I don't know what I could do with that xmlRoot anyway

alert(node3) gives: null
that means, the node with the id "masterblaster" wasn't found, although it exists:

<?xml version="1.0"?>
<!DOCTYPE graph PUBLIC "-//John Punin//DTD graph description//EN" "http://www.cs.rpi.edu/~puninj/XGMML/xgmml.dtd">

<graph directed="1" graphic="1" Layout="points">
<fuckit id="masterblaster" label="whatever"/>
<node id="1" label="Node 1" weight="0">
<graphics type="rhombus" x="270" y="90" ></graphics>
</node>
<node id="2" label="node 2" weight="0">
<graphics type="ver_ellipsis" x="350" y="190" ></graphics>
</node>
<edge source="1" target="2" weight="0" label="Edge from node 1 to node 2" ></edge>
</graph>


The attempt to get the label of node3 obviously fails as well.

Hints, anyone? :)

tia,
moritz

paxineintaxi
05-01-2004, 06:56 AM
hi again,

I can access the tag <fu..it> and its label using this code:


xmlDoc = parseXML(urlRequestStatus.content, null); // null -> create a new XML doc
var nodes = xmlDoc.getElementsByTagName("fuckit");
alert(nodes.item(0).getAttribute("label")); // gives "whatever"
alert(nodes.item(0).getAttribute("id")); // gives "masterblaster"



What makes me curious is, that the method getElementById() can find the
<fu..it>-tag with the id masterblaster, although it exists. :?: :?

Any ideas?

bye,
moritz

jafro - svg
05-01-2004, 07:34 AM
Hi, having fun with ParseXML and getElementID, eh.

This should work.

var xmlRoot = xmlDoc.getDocumentElement();
masterblaster=xmlDoc.getElementById("masterblaster");
if (masterblaster.hasAttribute("viewBox")) {
label1 = masterblaster.getAttribute("label");
}
alert(label1);

Or try this:
attrs = masterblaster.getAttributes();
attrsLen = attrs.getLength();
for (i = 0; i < attrsLen; i++) {
attrNode = attrs.item(i);
alert(attrNode.getNodeName());
alert(attrNode.getNodeValue());

Glad that the fu..it tag makes it through our filter. Your employer must be fairly liberal ;P

If this does not work let me know and I will take the time to test... i'm off to get some breakfast.

Best,

Learn SVG - http://learnsvg.com

paxiineintaxi
05-01-2004, 01:01 PM
hi jafro!

Hi, having fun with ParseXML and getElementID, eh.

Jeah, but there's a serious component. When I fail in getting this little
example to work, our team might decide to use Flash. :twisted:

My sentence from the second posting:

What makes me curious is, that the method getElementById() can find the
<fu..it>-tag with the id masterblaster, although it exists.

Should be replaced by:

What makes me curious is, that the method getElementById() can't find the
<fu..it>-tag with the id masterblaster, although it exists.

Note the change from "can" to "can't". :D

Back to the problem:

xmlDoc = parseXML(urlRequestStatus.content, null);
alert(xmlDoc);

alert() tells me, that xmlDoc in an object of type "Document". And
according to http://www.adobe.com/svg/indepth/pdfs/CurrentSupport.pdf,
the "Document"-object has a method "getElementById". This is true, since

var node = xmlDoc.getElementById("masterblaster");

executes and returns. Unfortunately it always returns "null" when I use it
with my custom xmlDoc. But I can apply "getElementById" without any problems
on the svgDocument. For example:

var text_node = svgDoc.getElementById("thetext");
alert(text_node);

alert() tells me, that text_node is an object of type "SVGTextElement".

Are there any conditions, that have to be fulfilled for an XML-document,
when I want to use getElementById() ?

[... researching ...] :wink:

Hey, I found something in the W3C's DOM-specifications:

http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/core.html


getElementById introduced in DOM Level 2
Returns the Element whose ID is given by elementId. If no such element exists, returns null. Behavior is not defined if more than one element has this ID.

Note: The DOM implementation must have information that says which attributes are of type ID. Attributes with the name "ID" are not of type ID unless so defined. Implementations that do not know whether attributes are of type ID or not are expected to return null.


OK, I've never defined the attribute that contains the ID. That should be the reason,
why getElementId does not work with my custom XML-file.

But how can I define the ID-attribute?

Grateful for some hints,

moritz

Anonymous
05-02-2004, 10:42 AM
Hi moritz

i got it to work like this:

getURL("some.xml", my_callback);

function my_callback( urlRequestStatus ) {

if (urlRequestStatus.success)
{

xmlDoc = parseXML(urlRequestStatus.content, document);
document.documentElement.appendChild(xmlDoc)
var node3 = document.getElementById("masterblaster");
var label3 = node3.getAttribute("label");
alert(label3);
}
else
{
alert('nuttin\' loaded');
}
}
if you do not create a new document,but append the loaded nodes to your Document,
u can use the documents getElementById method.
this way it works, but i think it would be usefull to use your own namespace for your tags,
in this case.
hope that saves you from using flash!
holger

paxiineintaxi
05-03-2004, 06:21 AM
Hi Holger,

thanks a lot for your hint. When I exclude the <? xml ...> and <!DOCTYPE ... >
stuff from the XML-document, your approach works very well. :D

Meanwhile, I managed to do what I wanted to do without the getElementById-method.
Of course, it's good to know that I can use it when I need it.

But instead of appending the XML-tree to svgDoc.documentElement, it might be better
to append it to a <metadata>-tag. http://www.w3.org/TR/SVG/metadata.html :?:

Bye,

Moritz

 
Web mp2kmag.com
mapforums.com

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum