Matlab xml解析提供空元素

时间:2014-12-16 14:28:16

标签: xml matlab xml-parsing

我有一个具有以下结构的xml文件

<?xml version='1.0' encoding='us-ascii'?>

<config_Paramters name="Model">
  <joint name = "number_1">
    <parameter>
      <joint param = "MINNUMBER1"   value = "-1*M_PI/180" units = "in rad" desc = "Max ang 1"/>
      <joint param = "MAXNUMBER1"   value = "-2*M_PI/180" units = "in rad" desc = "Min ang 1"/>
    </parameter>
  </joint>
  <joint name = "number_2">
    <parameter>
      <joint param = "MINNUMBER2"   value = "1*M_PI/180" units = "in rad" desc = "Max ang 2"/>
      <joint param = "MAXNUMBER2"   value = "0*M_PI/180" units = "in rad" desc = "Min ang 2" />
    </parameter>
  </joint>
</config_Paramters>

我想访问节点,但以下代码返回空元素:

filename = 'settings.xml';
xDoc = xmlread(filename);
nodeList = xDoc.getElementsByTagName('joint');
firstNode = nodeList.item(0)
firstNodeContent = firstNode.getTextContent

这个不应该有效吗?如果有人能告诉我这个问题会很棒。

1 个答案:

答案 0 :(得分:0)

您的问题是您的'joint'节点内没有文本内容,只有其他节点,因此.getTextContent不返回任何内容。文本内容看起来像这样:

<?xml version='1.0' encoding='us-ascii'?>

<config_Paramters name="Model">
  <joint name = "number_1">
    Hello
  </joint>
  <joint name = "number_2">
    World!
  </joint>
</config_Paramters>

如果您要在问题中构建XML,则需要根据需要继续提取单个节点和属性。您可能还对MathWorks的文件交换中的各种解决方案感兴趣,该解决方案将XML文件转换为Matlab struct,例如thisthis - 两者都称为xml2struct。如果您有生物信息学工具箱,那么该名称中还包含一个记录不多的功能。