XML :: Parsing - 无法检测子节点

时间:2017-10-23 14:51:20

标签: xml perl xml-parsing xml-libxml

示例XML文档

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    print("numberOfRows Call")
    if places.count < self.numberPlaces {
        return places.count   /*  rows   */
    } 
    return  self.numberPlaces
}

解析上面的XML文档的代码。

<?xml version="1.0" encoding="UTF-8"?>
<web-interface-classifier xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:noNamespaceSchemaLocation="WEB-INTERFACE-GROUP-CLASSIFIER.xsd" xmlns="parent/child1/granchild2/v1">
  <classifier>
      <key1>somevalue</key1>
  </classifier>
  <classifier>
      <key2>somevalue</key2>
  </classifier>
</web-interface-classifier>

我试图解析XML文档并从&#39;分类器&#39;转储所需的键值对。节点,未检测到所需的子节点。你能提供一些指示吗?

1 个答案:

答案 0 :(得分:4)

您正试图从aTimer.Elapsed -= new ElapsedEventHandler(OnTimedEvent);命名空间中找到web-interface-classifierclassifier类型的元素,但您要求查找parent/child1/granchild2/v1web-interface-classifier类型的元素来自null命名空间。修正:

classifier

输出:

#!/usr/bin/env perl

use strict;
use warnings;
use v5.10;

use Data::Dumper;
use XML::LibXML;
use XML::LibXML::XPathContext;

my $dom = XML::LibXML->load_xml( IO => \*DATA );

my $xpc = XML::LibXML::XPathContext->new();
$xpc->registerNs( u => "parent/child1/granchild2/v1" );

for my $node ( $xpc->findnodes( "//u:web-interface-classifier/u:classifier", $dom ) ) {
    say $node->toString();
}

__DATA__
<?xml version="1.0" encoding="UTF-8"?>
<web-interface-classifier xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:noNamespaceSchemaLocation="WEB-INTERFACE-GROUP-CLASSIFIER.xsd" xmlns="parent/child1/granchild2/v1">
  <classifier>
      <key1>somevalue</key1>
  </classifier>
  <classifier>
      <key2>somevalue</key2>
  </classifier>
</web-interface-classifier>