1

我正在使用 XML::LibXML(仅限于 v1.58)。感谢博罗丁这篇文章中我正确地获得了PROPERTY NAME="Name"if的值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>
4

1 回答 1

1

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

这段代码符合这个想法。它查找数据中所有具有属性等于的子VALUE.OBJECT元素的元素。对于这些元素,使用 显示整个元素的 XML,通过查找子元素,然后查找属性等于的子元素来找到并显示名称。INSTANCECLASSNAMESharedGtTranslatortoStringINSTANCEPROPERTYNAMEName

我还改变了从文件中解析数据的方式,因为你说你被一个XML::LibXML没有load_xml方法的版本卡住了。

请注意,该findnodes方法将始终返回节点列表,如果调用在列表上下文中,则作为 Perl 列表,如果XML::LibXML::NodeList在 scdalar 上下文中,则作为对象。无论 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";
}
于 2013-04-24T16:01:13.820 回答