使用Perl / LibXML获取子项与条件匹配的XML父元素

时间:2013-04-24 12:24:10

标签: xml perl xml-libxml

我正在使用XML :: LibXML(限于v1.58)。 感谢Borodin帖子中的this我正确获取了PROPERTY NAME="Name" CLASSNAME="SharedGtTranslator"的值。 我想补充的是获取包含上述内容的整个VALUE.OBJECT父元素,并将其输出到另一个XML文件。

因此,如果XML文件如下所示:

<CIM CIMVERSION="2.0" DTDVERSION="2.0">

  <DECLARATION>
    <DECLGROUP>
      <LOCALNAMESPACEPATH>
        <NAMESPACE NAME="ecm"/>
      </LOCALNAMESPACEPATH>

      <!-- Ecm -->

      <VALUE.OBJECT>
        <INSTANCE CLASSNAME="Ecm">
          <PROPERTY NAME="FileName" TYPE="string">
            <VALUE>/opt/sign/log/ecm.log</VALUE>
          </PROPERTY>
          <PROPERTY NAME="Log" TYPE="sint32">
            <VALUE>0</VALUE>
          </PROPERTY>
          <PROPERTY NAME="Name" TYPE="string">
            <VALUE>ECM</VALUE>
          </PROPERTY>
          <PROPERTY NAME="TimerClaim" TYPE="sint32">
            <VALUE>5000</VALUE>
          </PROPERTY>
          <PROPERTY NAME="TimerConnect" TYPE="sint32">
            <VALUE>8000</VALUE>
          </PROPERTY>
          <PROPERTY NAME="TimerNegotiate" TYPE="sint32">
            <VALUE>5000</VALUE>
          </PROPERTY>
          <PROPERTY NAME="TimerReconnect" TYPE="sint32">
            <VALUE>500</VALUE>
          </PROPERTY>
        </INSTANCE>
      </VALUE.OBJECT>

      <VALUE.OBJECT>
        <INSTANCE CLASSNAME="Ecm_Host">
          <PROPERTY.REFERENCE NAME="Parent">
            <VALUE.REFERENCE>
              <INSTANCENAME CLASSNAME="Ecm">
                <KEYBINDING NAME="Name">
                  <KEYVALUE>ECM</KEYVALUE>
                </KEYBINDING>
              </INSTANCENAME>
            </VALUE.REFERENCE>
          </PROPERTY.REFERENCE>
          <PROPERTY.REFERENCE NAME="Child">
            <VALUE.REFERENCE>
              <INSTANCENAME CLASSNAME="Host">
                <KEYBINDING NAME="Address">
                  <KEYVALUE>gtttcnss7v19</KEYVALUE>
                </KEYBINDING>
              </INSTANCENAME>
            </VALUE.REFERENCE>
          </PROPERTY.REFERENCE>
        </INSTANCE>
      </VALUE.OBJECT>

      <VALUE.OBJECT>
        <INSTANCE CLASSNAME="Ecm_ProcessClass">
          <PROPERTY.REFERENCE NAME="Parent">
            <VALUE.REFERENCE>
              <INSTANCENAME CLASSNAME="Ecm">
                <KEYBINDING NAME="Name">
                  <KEYVALUE>ECM</KEYVALUE>
                </KEYBINDING>
              </INSTANCENAME>
            </VALUE.REFERENCE>
          </PROPERTY.REFERENCE>
          <PROPERTY.REFERENCE NAME="Child">
            <VALUE.REFERENCE>
              <INSTANCENAME CLASSNAME="ProcessClass">
                <KEYBINDING NAME="ClassID">
                  <KEYVALUE>3</KEYVALUE>
                </KEYBINDING>
              </INSTANCENAME>
            </VALUE.REFERENCE>
          </PROPERTY.REFERENCE>
        </INSTANCE>
      </VALUE.OBJECT>

      <VALUE.OBJECT>
        <INSTANCE CLASSNAME="Ecm_ProcessClass">
          <PROPERTY.REFERENCE NAME="Parent">
            <VALUE.REFERENCE>
              <INSTANCENAME CLASSNAME="Ecm">
                <KEYBINDING NAME="Name">
                  <KEYVALUE>ECM</KEYVALUE>
                </KEYBINDING>
              </INSTANCENAME>
            </VALUE.REFERENCE>
          </PROPERTY.REFERENCE>
          <PROPERTY.REFERENCE NAME="Child">
            <VALUE.REFERENCE>
              <INSTANCENAME CLASSNAME="ProcessClass">
                <KEYBINDING NAME="ClassID">
                  <KEYVALUE>1</KEYVALUE>
                </KEYBINDING>
              </INSTANCENAME>
            </VALUE.REFERENCE>
          </PROPERTY.REFERENCE>
        </INSTANCE>
      </VALUE.OBJECT>

      <VALUE.OBJECT>
        <INSTANCE CLASSNAME="Ecm_ProcessClass">
          <PROPERTY.REFERENCE NAME="Parent">
            <VALUE.REFERENCE>
              <INSTANCENAME CLASSNAME="Ecm">
                <KEYBINDING NAME="Name">
                  <KEYVALUE>ECM</KEYVALUE>
                </KEYBINDING>
              </INSTANCENAME>
            </VALUE.REFERENCE>
          </PROPERTY.REFERENCE>
          <PROPERTY.REFERENCE NAME="Child">
            <VALUE.REFERENCE>
              <INSTANCENAME CLASSNAME="ProcessClass">
                <KEYBINDING NAME="ClassID">
                  <KEYVALUE>15</KEYVALUE>
                </KEYBINDING>
              </INSTANCENAME>
            </VALUE.REFERENCE>
          </PROPERTY.REFERENCE>
        </INSTANCE>
      </VALUE.OBJECT>
      <VALUE.OBJECT>
        <INSTANCE CLASSNAME="SharedGtTranslator">
          <QUALIFIER NAME="Configstate" TYPE="string">
            <VALUE>planned</VALUE>
          </QUALIFIER>
          <PROPERTY NAME="Name" TYPE="string">
            <VALUE>HLR$4,0,1,3,7240*-->HLR HLR LocalSPC: 410 SSN: 6</VALUE>
          </PROPERTY>
          <PROPERTY NAME="NatureOfAddress" TYPE="sint32">
            <VALUE>3</VALUE>
          </PROPERTY>
          <PROPERTY NAME="NetworkID" TYPE="sint32">
            <VALUE>1</VALUE>
          </PROPERTY>
          <PROPERTY NAME="NumberSeries" TYPE="sint32">
            <VALUE>7240*</VALUE>
          </PROPERTY>
          <PROPERTY NAME="NumberingPlan" TYPE="sint32">
            <VALUE>1</VALUE>
          </PROPERTY>
          <PROPERTY NAME="TranslationType" TYPE="sint32">
            <VALUE>0</VALUE>
          </PROPERTY>
        </INSTANCE>
      </VALUE.OBJECT>
      <VALUE.OBJECT>
        <INSTANCE CLASSNAME="SharedGtTranslator">
          <QUALIFIER NAME="Configstate" TYPE="string">
            <VALUE>planned</VALUE>
          </QUALIFIER>
          <PROPERTY NAME="Name" TYPE="string">
            <VALUE>HLR$4,0,1,3,7250*-->HLR HLR LocalSPC: 410 SSN: 6</VALUE>
          </PROPERTY>
          <PROPERTY NAME="NatureOfAddress" TYPE="sint32">
            <VALUE>3</VALUE>
          </PROPERTY>
          <PROPERTY NAME="NetworkID" TYPE="sint32">
            <VALUE>1</VALUE>
          </PROPERTY>
          <PROPERTY NAME="NumberSeries" TYPE="sint32">
            <VALUE>7250*</VALUE>
          </PROPERTY>
          <PROPERTY NAME="NumberingPlan" TYPE="sint32">
            <VALUE>1</VALUE>
          </PROPERTY>
          <PROPERTY NAME="TranslationType" TYPE="sint32">
            <VALUE>0</VALUE>
          </PROPERTY>
        </INSTANCE>
      </VALUE.OBJECT>
    </DECLGROUP>

  </DECLARATION>
</CIM>

新文件仅包含以下内容:

<CIM>
  <DECLARATION>
    <DECLGROUP>
          <VALUE.OBJECT>
            <INSTANCE CLASSNAME="SharedGtTranslator">
              <QUALIFIER NAME="Configstate" TYPE="string">
                <VALUE>planned</VALUE>
              </QUALIFIER>
              <PROPERTY NAME="Name" TYPE="string">
                <VALUE>HLR$4,0,1,3,7240*-->HLR HLR LocalSPC: 410 SSN: 6</VALUE>
              </PROPERTY>
              <PROPERTY NAME="NatureOfAddress" TYPE="sint32">
                <VALUE>3</VALUE>
              </PROPERTY>
              <PROPERTY NAME="NetworkID" TYPE="sint32">
                <VALUE>1</VALUE>
              </PROPERTY>
              <PROPERTY NAME="NumberSeries" TYPE="sint32">
                <VALUE>7240*</VALUE>
              </PROPERTY>
              <PROPERTY NAME="NumberingPlan" TYPE="sint32">
                <VALUE>1</VALUE>
              </PROPERTY>
              <PROPERTY NAME="TranslationType" TYPE="sint32">
                <VALUE>0</VALUE>
              </PROPERTY>
            </INSTANCE>
          </VALUE.OBJECT>
          <VALUE.OBJECT>
            <INSTANCE CLASSNAME="SharedGtTranslator">
              <QUALIFIER NAME="Configstate" TYPE="string">
                <VALUE>planned</VALUE>
              </QUALIFIER>
              <PROPERTY NAME="Name" TYPE="string">
                <VALUE>HLR$4,0,1,3,7250*-->HLR HLR LocalSPC: 410 SSN: 6</VALUE>
              </PROPERTY>
              <PROPERTY NAME="NatureOfAddress" TYPE="sint32">
                <VALUE>3</VALUE>
              </PROPERTY>
              <PROPERTY NAME="NetworkID" TYPE="sint32">
                <VALUE>1</VALUE>
              </PROPERTY>
              <PROPERTY NAME="NumberSeries" TYPE="sint32">
                <VALUE>7250*</VALUE>
              </PROPERTY>
              <PROPERTY NAME="NumberingPlan" TYPE="sint32">
                <VALUE>1</VALUE>
              </PROPERTY>
              <PROPERTY NAME="TranslationType" TYPE="sint32">
                <VALUE>0</VALUE>
              </PROPERTY>
            </INSTANCE>
          </VALUE.OBJECT>
    </DECLGROUP>

  </DECLARATION>
</CIM>

1 个答案:

答案 0 :(得分:1)

如果您需要VALUE.OBJECT结构,我建议您先找到它们,然后在其中查找为您提供名称的PROPERTY元素。

这个代码提出了这个想法。它会查找数据中包含VALUE.OBJECT个子INSTANCE属性等于CLASSNAME的所有SharedGtTranslator个元素。对于这些元素,使用toString显示整个元素的XML,并通过查找INSTANCE子项,然后查找具有“{1}}子项的PROPERTY子项来查找和显示名称NAME属性等于Name

我还改变了从文件中解析数据的方式,因为您说您遇到的XML::LibXML版本没有load_xml方法。

请注意,findnodes方法将始终返回节点列表,如果调用位于列表上下文中,则返回Perl列表;如果位于scdalar上下文中,则返回XML::LibXML::NodeList对象。无论XPath表达式有多少,一个还是零匹配,都是如此,如果您有任何疑问,应该检查在继续处理之前找到的节点数。

use strict;
use warnings;

use XML::LibXML;

my $doc = XML::LibXML->new(no_blanks => 1)->parse_file($file1);

my @value_objects = $doc->findnodes('//VALUE.OBJECT[INSTANCE[@CLASSNAME = "SharedGtTranslator"]]');

for my $value_object (@value_objects) {

  print "\n";
  print $value_object->toString(1), "\n";
  print "\n";

  my @names = $value_object->findnodes('INSTANCE/PROPERTY[@NAME = "Name"]');
  print $names[0]->textContent, "\n";
}