简体   繁体   English

JavaScript解析XML

[英]JavaScript parse XML

I try to parse an XML string and have some problems. 我尝试解析XML字符串,但遇到一些问题。 Here is my current state. 这是我目前的状态。 I have a Cordova app which reads QR-Codes (with the BarcodeScanner plugin). 我有一个Cordova应用程序,可读取QR码(带有BarcodeScanner插件)。 The QR-Code holds the XML information. QR码保存XML信息。 When I read the code, I want to print out the XML code. 读取代码时,我想打印出XML代码。 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 我的XML代码如下所示

<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. 并使用它代替标记行中的“ result.text”,一切都会按预期进行。

So maybe the 'result.text' is just a reference and not the value? 因此,也许“ result.text”只是参考,而不是值? Could this be the problem? 这可能是问题吗? I'm not an expert so how can I solve this problem? 我不是专家,如何解决此问题?

PS: The XML code I get from the QR-Code is correct and well formed. PS:我从QR代码获得的XML代码正确且格式正确。

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 result.text中缺少“ e”

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. 因此,“错误”输出只是HTML格式的错误消息,我在其中打印每个标签。 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 但是在创建QR代码反斜杠时会添加

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

The first backslash is at column 14. 第一个反斜杠在第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. 当我创建一个静态XML字符串时,我插入了反斜杠以屏蔽“”,因此我的声明和QR代码中的XML代码看起来相等。但是它们不相等,因为静态XML字符串不包含反斜杠。这些反斜杠在解析时会导致错误。

The easiest solution is to just not put the XML information in the QR-Code. 最简单的解决方案是不将XML信息放入QR码中。 So directly starting with the first node. 因此直接从第一个节点开始。

Thanks for your help. 谢谢你的帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM