简体   繁体   English

getChildNodes() 返回 Text 而不是 NodeList

[英]getChildNodes() returns Text instead of NodeList

I'm trying to do XML parsing in MATLAB, which uses JavaSE package org.w3c.dom我正在尝试在使用 JavaSE 包 org.w3c.dom 的 MATLAB 中进行 XML 解析

I have a xml structure like我有一个像这样的 xml 结构

 <?xml version="1.0" encoding="utf-8"?> <tada> <Filters> <filter id="1" name="name1" renderType="type1"> <bool name="par1" value="false"/> <bool name="par2" value="false"/> <int name="par3" value="200"/> <int name="par4" value="1"/> <float name="par5" value="2"/> <bool name="par6" value="false"/> <int name="par7" value="1"/> </filter> <filter id="6" name="name2" renderType="type2"> <bool name="par1" value="false"/> <bool name="par2" value="false"/> <int name="par3" value="200"/> <int name="par4" value="1"/> <float name="par5" value="2"/> <bool name="par6" value="false"/> <int name="par7" value="1"/> <int name="par8" value="1"/> <array name="par9" row="2" col="2" > 5.0162 0.96182 0.22294 0.96356 </array> </filter> </Filters> </tada>

I'm trying to get access for, let say attribute renderType of element filter with name="name2"我正在尝试访问,比如name="name2"元素过滤器的属性renderType

I'm trying to do the following我正在尝试执行以下操作

 config = xmlread(xmlFileName); section = config.getDocumentElement().getElementsByTagName('Filters').item(0); filters = section.getChildNodes(); // filters = section.getElementsByTagName('filter'); for i = 0:filters.getLength()-1 filter = filters.item(i); if strcmp(filter.getAttribute('name'), 'name2') filter.setAttribute('renderType', 'graph2D'); end end

I expect that filters should be a NodeList (as documentation says http://docs.oracle.com/javase/6/docs/api/org/w3c/dom/package-summary.html?browser=F1help ), but it's returned as a Text (which contains only white spaces).我希望过滤器应该是一个NodeList (如文档所述http://docs.oracle.com/javase/6/docs/api/org/w3c/dom/package-summary.html?browser=F1help ),但它返回作为文本(仅包含空格)。

If I write如果我写

 filters = section.getElementsByTagName('filter');

then I get desired NodeList , but the problem is that this way is unsuitable.然后我得到了想要的NodeList ,但问题是这种方式不合适。 Because I need to select nodes by the value of name property.因为我需要通过name属性的值来选择节点。

Could you help me?你可以帮帮我吗? What I do wrong?我做错了什么?

Thank you for your help.感谢您的帮助。

Your XML document includes whitespace between the elements.您的 XML 文档在元素之间包含空格。 This is considered significant by the XML parser, and when you use the DOM method getChildNodes() it includes both the child elements and the child text nodes containing the whitespace. XML 解析器认为这很重要,当您使用 DOM 方法 getChildNodes() 时,它包括子元素和包含空格的子文本节点。 When processing the child nodes, if you want to ignore the whitespace text nodes you must do so explicitly.在处理子节点时,如果您想忽略空白文本节点,您必须明确这样做。

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

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