简体   繁体   English

解析带有Javascript的“非标准” XML?

[英]Parse “Non-Standard” XML with Javascript?

Good afternoon, 下午好,

I am trying to parse through an XML file with strictly Javascript (I would prefer to not use an add on such as jQuery because of memory constraints in the module this codes resides in). 我试图使用严格的Javascript通过XML文件进行解析(由于该代码所驻留的模块中的内存限制,我不希望使用jQuery之类的add插件)。 The XML file is populated by a module within a control system I have in an assembly line controller so I think the format is a little different that typical XML files. XML文件由组装线控制器中控制系统中的模块填充,因此我认为格式与典型XML文件略有不同。 I can load the file correctly (I used an alert to see the raw text output and it is correct), but I can't load the value of the tag. 我可以正确加载文件(我使用了警报来查看原始文本输出,它是正确的),但是我无法加载标签的值。 A portion of the XML file loaded is below: 加载的XML文件的一部分如下:

<tag name = "Parts_Built_Buffer[490].DateTime" valueType="cip:dt_STRINGI" path="1,0" access="read" display="String">
    <value xsi:type="cip:dt_STRINGI">20140804110319</value>
</tag>

And it continues for 500 such tags... Every tag has a unique name due to the nature of the module requirements, so I want to loop through all the tags to get the values and build a table. 并继续提供500个这样的标签...由于模块要求的性质,每个标签都有一个唯一的名称,因此我想遍历所有标签以获取值并建立表。 For this my code (minus the loop part as I need to crawl (load 1 value) before I run (load all values)) is: 为此,我的代码(减去循环部分,因为在我运行(加载所有值)之前需要爬网(加载1个值))是:

var builtDataView_url = "/user/system/dataviews/Parts_Built_Buffer.xml";

    var getDataView = new XMLHttpRequest();
    getDataView.open("GET", builtDataView_url, false);
    getDataView.send();
    var builtXML = getDataView.responseXML;
    alert("response is " + getDataView.responseText)

    var job0 = builtXML.getElementsByTagName("/tag[Name='Parts_Built_Buffer[0].DateTime']/value")[0];
    alert("job0 is " + job0)

In the first alert I get a text view of the XML and it appears as expected. 在第一个警报中,我得到XML的文本视图,它按预期显示。 However, for job0 I get that it is undefined (if I alert job0.length I get 0). 但是,对于job0,我得到它是未定义的(如果我警告job0.length,则得到0)。 I need this to work in both Chrome and IE. 我需要在Chrome和IE中都可以使用。 Any advice will be greatly appreciated. 任何建议将不胜感激。

EDIT: I got to play around in the console some today (I ran out of time yesterday and this morning) with the data returned. 编辑:今天我必须在控制台中玩一些(昨天和今天早晨我没时间了)返回的数据。 I am fairly new to javascript and am self (ie Google) taught. 我对javascript很陌生,并且自学(即Google)教学。 The console dialog is below, the XML returned is not grouping the value tag with the parts_built... tag. 控制台对话框在下面,返回的XML没有将value标签与parts_built ...标签分组。 Since I am new to this, no making fun of my attempts to read the data if the function calls don't make sense, please... 由于我是新手,如果函数调用没有意义,请不要取笑我读取数据的尝试,请...

data.getElementsByTagName("*")
HTMLCollection[101]
data.getElementsByTagName("*")[0]
<view xmlns:xsi=​"http:​/​/​www.w3.org/​2001/​XMLSchema-instance" xmlns=​"http:​/​/​    www.rockwellautomation.com/​technologies/​data_access/​data_views/​1.0/​" xmlns:cip=​"http:​/​/​www.rockwellautomation.com/​technologies/​data_access/​data_types/​1.0/​" xsi:schemaLocation=​"http:​/​/​www.rockwellautomation.com/​technologies/​data_access/​data_views/​1.0/​ /​schema/​  dataview.xsd" name=​"EWEB_Data" description>​…​</view>​
data.getElementsByTagName("*")[1]
<tag name=​"Parts_Built_Buffer[490]​.DateTime" valueType=​"cip:​dt_STRINGI" path=​"1,0"    access=​"read" display=​"String">​
<value xsi:type=​"cip:​dt_STRINGI">​20140805111641​</value>​
</tag>​
data.getElementsByTagName("*")[1].value
undefined
data.getElementsByTagName("*")[2]
<value xsi:type=​"cip:​dt_STRINGI">​20140805111641​</value>​
data.getElementsByTagName("*")[3]
<tag name=​"Parts_Built_Buffer[490]​.VIN" valueType=​"cip:​dt_STRINGI" path=​"1,0" access=​    "read" display=​"String">​…​</tag>​
data.getElementsByTagName("*")[4]
<value xsi:type=​"cip:​dt_STRINGI">​</value>​
data.getElementsByTagName("*")[5]
<tag name=​"Parts_Built_Buffer[490]​.SerNum" valueType=​"cip:​dt_STRINGI" path=​"1,0"     access=​"read" display=​"String">​
<value xsi:type=​"cip:​dt_STRINGI">​643Y037S14217​</value>​
</tag>​
data.getElementsByTagName("*")[6]
<value xsi:type=​"cip:​dt_STRINGI">​643Y037S14217​</value>​
data.getElementsByTagName("*")[5].getElementsByTagName("*")
[
<value xsi:type=​"cip:​dt_STRINGI">​643Y037S14217​</value>​
]
data.getElementsByTagName("tag name*")[5].getElementsByTagName("*")
TypeError: Cannot read property 'getElementsByTagName' of undefined
data.getElementsByTagName("Parts_Built_Buffer*")[5].getElementsByTagName("*")
TypeError: Cannot read property 'getElementsByTagName' of undefined

This code will produce the value (keep in mind I only care about the even tag indices as they hold the values... data.getElementsByTagName("*")[6].textContent 此代码将产生值(请记住,我只关心偶数标签索引,因为它们包含值... data.getElementsByTagName(“ *”)[6] .textContent

This seems very clunky though so any advice will still be appreciated... At least it looks like I'm not going to have to start with the text output and parse through a long text string... 尽管这似乎很笨拙,所以任何建议仍然值得赞赏...至少看起来我不必从文本输出开始并解析一个长文本字符串...

You can use the standard DOM properties: childNodes , firstChild , nextSibling . 您可以使用标准的DOM属性: childNodesfirstChildnextSibling

So, I would navigate to the node that contains all tags, and then I would use a for loop on the childNodes . 因此,我将导航到包含所有标签的节点,然后在childNodes上使用for循环。 Something similar to this: 类似于以下内容:

var main_node = something;
var table = {};
for (var i = 0; i < main_node.childNodes.length; i++) {
    var node = main_node.childNodes[i];
    if (node.nodeType == 1) {
        // Insert your code here.
        var name = node.getAttribute('name');
        var value = writeYourFunctionToExtractValueFromNode(node);
        table[name] = value;
    }
}

// After the pre-processing step above, you can just use:
alert(table['Parts_Built_Foobar']);

The check for nodeType is to skip comments, whitespace, and other non-relevant nodes. nodeType的检查是跳过注释,空格和其他不相关的节点。

Everything I mentioned here is just standard DOM, and is available essentially everywhere (including in languages other than JavaScript). 我在这里提到的所有内容都只是标准DOM,并且基本上可以在任何地方使用(包括JavaScript以外的语言)。

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

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