¿Por qué no puedo acceder a elementos dentro de un archivo XML con XPath en XML :: LibXML?

StackOverflow https://stackoverflow.com/questions/2079668

Pregunta

Tengo un archivo XML, parte de la cual se ve así:

 <wave waveID="1">
    <well wellID="1" wellName="A1">
      <oneDataSet>
        <rawData>0.1123975676</rawData>
      </oneDataSet>
    </well>
    ... more wellID's and rawData continues here...

Estoy tratando de analizar el archivo con LibXML de Perl y la salida de la wellName y la rawData utilizando la siguiente:

    use XML::LibXML;
    my $parser = XML::LibXML->new();
    my $doc = $parser->parse_file('/Users/johncumbers/Temp/1_12-18-09-111823.orig.xml');
    my $xc = XML::LibXML::XPathContext->new( $doc->documentElement()  );
    $xc->registerNs('ns', 'http://moleculardevices.com/microplateML');

            my @n = $xc->findnodes('//ns:wave[@waveID="1"]');   #xc is xpathContent
        # should find a tree from the node representing everything beneath the waveID 1
        foreach $nod (@n) {
            my @c = $nod->findnodes('//rawData');  #element inside the tree.
            print @c;
        }

No está imprimiendo nada en este momento y creo que tengo un problema con mis declaraciones XPath. Por favor, puede ayudar a corregir el problema, o que me puede mostrar cómo disparar problemas declaraciones XPath? Gracias.

¿Fue útil?

Solución

En lugar de utilizar findnodes en el bucle, utilice getElementsByTagName () :

my @c = $nod->getElementsByTagName('rawData');

Aquí están algunos otros métodos práctico de usar de proceso para @c array:

$c[0]->toString;    # <rawData>0.1123975676</rawData>
$c[0]->nodeName;    # rawData
$c[0]->textContent; # 0.1123975676

Otros consejos

Si el elemento 'ola' se encuentra en un espacio de nombres a continuación, el elemento 'rawData' es también así que es probable que necesite usar

foreach $nod (@n) {
    my @c = $xc->findnodes('descendant::ns:rawData', $nod);  #element inside the tree.
    print @c;
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top