0

Alright, so I got this xml file:

<?xml version="1.0" encoding="UTF-8" ?> 
<level>
    <tiles>
        <row>1000000000000001</row>
        <row>1000000000000001</row>
        <row>1000000000000001</row>
        <row>1000000000000001</row>
        <row>1000000000000001</row>
        <row>1000000000000001</row>
        <row>1000000000000001</row>
        <row>1111111111111111</row>
    </tiles>
</level>

and I got my XML reading code:

var xmlDoc = document.implementation.createDocument("","",null);

and

            function loadXML(){
                xmlDoc.load("levels/test.xml");
                xmlDoc.onload = readLevel();
            }
            function readLevel() {


                throw(xmlDoc);

                if(xmlDoc.getElementsByTagName("tiles")[0].hasChildNodes()){
                    var rowNum = xmlDoc.getElementsByTagName("tiles").getChildNodes();
                    level = [];
                    for(var i = 0; i < rowNum; i++){
                        level[i] = [];
                        var tempStr = xmlDoc.getElementsByTagName("tiles").childNodes[i].textContent;
                        for(var j = 0; j < 16; j++){
                            level[i][j] = parceInt(tempStr.charAt(j));
                        }

                    }
                }

                for (var i = 0; i < level.length; i++) {
                    blockArray[i] = []; // Create the second level for this index
                    for (var j = 0; j < level[i].length; j++) {
                        var tempImg = new Image();
                        tempImg.src = "images/block" + level[i][j] + ".png";
                        blockArray[i][j] = new block(j * blockSize, i * blockSize, level[i][j], false, false, tempImg);
                        //throw('blockArray['+i+']'+j+'] = ' + level[i][j]);
                    }
                }
            }

Now why isn't this working? It constantly says xmlDoc.getElementsByTagName("tiles")[0] is undefined and that xmlDoc.getElementsByTagName("tiles").length = 0. So what am I doing wrong?

3
  • 2
    AFAIK, there shouldn't be a space between < and ?. How is xmlDoc being populated? And why do you throw an exception? Commented Jan 23, 2011 at 21:30
  • added space, nothing happened. Commented Jan 23, 2011 at 21:34
  • 3
    No, "shouldn't be a space" means you'll have to remove it. ;-) Commented Jan 23, 2011 at 21:35

2 Answers 2

1

I'd use XMLHttpRequest and its responseXML property instead, which will work in all major browsers. Example:

function readLevel(xmlDoc) {
    alert(xmlDoc.documentElement.tagName);
    // Your existing code goes here
};

var createXmlHttpRequest = (function() {
    var factories = [
        function() { return new XMLHttpRequest(); },
        function() { return new ActiveXObject("Msxml2.XMLHTTP.6.0"); },
        function() { return new ActiveXObject("Msxml2.XMLHTTP.3.0"); },
        function() { return new ActiveXObject("Microsoft.XMLHTTP"); }
    ];

    for (var i = 0, len = factories.length; i < len; ++i) {
        try {
            if ( factories[i]() ) return factories[i];
        } catch (e) {}
    }
})();

var xmlHttp = createXmlHttpRequest();
xmlHttp.onreadystatechange = function() {
    if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
        readLevel(xmlHttp.responseXML);
    }
};

xmlHttp.open("GET", "levels/test.xml", true);
xmlHttp.send(null);
Sign up to request clarification or add additional context in comments.

2 Comments

Sorry, but in this code it seems readLevel never gets called.
@CyanPrime: How do you mean? Do you mean by reading it or running it? readLevel() is called in the onreadystatechange handler. I've tried it and it works in all major browsers.
0

According to SitePoint, all the arguments are required in createDocument. Maybe the falsy values are tripping you up.

6 Comments

Okay, I tried that and got this: An attempt was made to create or change an object in a way which is incorrect with regard to namespaces" code: "14
Using this code: var xmlDoc = document.implementation.createDocument("w3.org/1999/xhtml",'xml:lang',null);
@CyanPrime – Also see MDC.
Now using this var xmlDoc = document.implementation.createDocument('w3.org/XML/1998/namespace', 'xml:lang',null); still not working
That namespace is reserved for (specific) element names starting with <xml:...>. You shouldn't need to specify a namespace at all.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.