Вопрос

I try to parse an XML string and have some problems. Here is my current state. I have a Cordova app which reads QR-Codes (with the BarcodeScanner plugin). The QR-Code holds the XML information. When I read the code, I want to print out the XML code. Here is what I tried (the important part):

var app = {
    output: null, 
    xmlDoc: null,

    // this function is called when I click a button
    scanCode: function(){
        //first parameter is a callback, which is called when a barcode is detected
        cordova.plugins.barcodeScanner.scan(
            function (result) {
                alert(result.text);
                var parser = new DOMParser();

                **app.xmlDoc = parser.parseFromString(result.text,"text/xml");**

                app.output = document.getElementById("codeInfo");
                app.traverse(app.xmlDoc.documentElement, "");
            }, 
            function (error) {
                alert("Scanning failed: " + error);
            }
        );
    },

    traverse: function(node, offset){       
        if(node.nodeType == 3){
            app.output.innerHTML += "<b>" + offset + node.nodeValue + "</b><br>";
        }else{
            app.output.innerHTML += offset + node.nodeName + "<br>";
            var childs = node.childNodes;
            for(var i=0; i<childs.length; i++){
                app.traverse(childs[i], offset + "&nbsp;&nbsp;&nbsp;");
            }
        }
    }
};

My XML code looks something like this

<node><child1>text1</child1><child2>text2</child2></node>

So I expect an output like:

node
    child1
        text1
    child2
        text2

But I always get something like:

html
    body
        parsererror
            h3
                This page contains the following errors:
...

When I use a static text like

var xml = "<node><child1>text1</child1><child2>text2</child2></node>"

and use this instead of 'result.text' in the marked line, everything works as expected.

So maybe the 'result.text' is just a reference and not the value? Could this be the problem? I'm not an expert so how can I solve this problem?

P.S.: The XML code I get from the QR-Code is correct and well formed.

Это было полезно?

Решение 2

After reading Valencia's comment and the "wrong" output again and think about it, I figured out what's wrong. So the "wrong" output is just an error message in HTML format, where i print every tag. The message itself says:

This page contains the following errors:
error on line 1 at column 14: String not started expectin ' or "

The beginning should look like this

<?xml version="1.0" encoding="UTF-8"?>

but when creating the QR-Code backslashes are added

<?xml version=\"1.0\" encoding=\"UTF-8\"?>

The first backslash is at column 14.

When I create a static XML string I insert backslashes to mask the ' " ', so my declaration and the XML code from the QR-Code look equal. But they aren't, cause the static XML string does not contain the backslashes. And these backslashes cause the error while parsing.

The easiest solution is to just not put the XML information in the QR-Code. So directly starting with the first node.

Thanks for your help.

Другие советы

app.xmlDoc = parser.parseFromString(result.txt,"text/xml"); 

should actually be:

app.xmlDoc = parser.parseFromString(result.text,"text/xml"); 

There is an "e" missing in result.text

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top