Hi
I have looked and tried everything and i still cant sort this out i have no idea why this wont work
I am using Xerces Perl on Windows and Debian to try this and it wont work on both of them.
Basically i am tryin to do simple tree transversale and get the node values out
I now understand that evetything in DOM is a node including the actual element values etc so i am using the code below , part done myself part copied from the examples in the /t dir.
I dont get any values from this code, all i get are blanks and thats it, if i do getNodeName instead i get "#text". From the commented out code below this it shows i have tried many different things. I even tried to copy the code in DOM2Hash.pl but i couldt get it to work for my code.
Someone must have an answer.
----------------------------- code
use XML::Xerces;
use Getopt::Long;
use Benchmark;
package MyNodeFilter;
use vars qw(@ISA);
@ISA = qw(XML::Xerces:
erlNodeFilter);
$validate = $XML::Xerces::AbstractDOMParser::Val_Always;
### set new parser
my $DOM = XML::Xerces::XercesDOMParser->new;
$sc = 'lr.xsd';
$DOM->setValidationScheme ($sc);
$DOM->setDoNamespaces (1);
$DOM->setDoSchema (1);
$file = 'lr.xml';
$error_handler = XML::Xerces:
erlErrorHandler->new;
$DOM->setErrorHandler($error_handler);
$d = $DOM->parse($file);
XML::Xerces::error($@) if $@;
$doc = $DOM->getDocument;
$root = $doc->getDocumentElement;
$filter = MyNodeFilter->new;
$what = $XML::Xerces:
OMNodeFilter::SHOW_ALL;
$walker = $doc->createTreeWalker($root,$what,$filter,1);
XML::Xerces::error($@) if $@;
$node = $walker->getCurrentNode;
#$cd = $cn->getFirstChild;
if ($node->hasChildNodes) {
my $text;
foreach my $child ($node->getChildNodes) {
if ($child->getNodeType ne
$XML::Xerces:
OMNode::ELEMENT_NODE) {
print "sda";
print $child->getNodeValue;
}
# if ($child->isa('XML::Xerces:
OMElement')) {
# $k .= $child->getNodeValue;
# }
# if ($child->isa('XML::Xerces:
OMText')) {
# print "asd";
# print $k;
# $new{text} = $k;
# }
# $v = $child-> getElementsByTagName($k);
# print $v;
#}
## if $child->isa('XML::Xerces:
OMText');
}
}
# print $text;
# $return->{text} = $text
# if $text !~ /^\s*$/;
--------------------------------- lr.xml
<?xml version="1.0"?>
<room xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation='lr.xsd'> <roomref>ccccccccccccccc</roomref> <roomday>2004-06-30Z</roomday> <roomprice>229.00</roomprice> </room>
I have looked and tried everything and i still cant sort this out i have no idea why this wont work
I am using Xerces Perl on Windows and Debian to try this and it wont work on both of them.
Basically i am tryin to do simple tree transversale and get the node values out
I now understand that evetything in DOM is a node including the actual element values etc so i am using the code below , part done myself part copied from the examples in the /t dir.
I dont get any values from this code, all i get are blanks and thats it, if i do getNodeName instead i get "#text". From the commented out code below this it shows i have tried many different things. I even tried to copy the code in DOM2Hash.pl but i couldt get it to work for my code.
Someone must have an answer.
----------------------------- code
use XML::Xerces;
use Getopt::Long;
use Benchmark;
package MyNodeFilter;
use vars qw(@ISA);
@ISA = qw(XML::Xerces:
$validate = $XML::Xerces::AbstractDOMParser::Val_Always;
### set new parser
my $DOM = XML::Xerces::XercesDOMParser->new;
$sc = 'lr.xsd';
$DOM->setValidationScheme ($sc);
$DOM->setDoNamespaces (1);
$DOM->setDoSchema (1);
$file = 'lr.xml';
$error_handler = XML::Xerces:
$DOM->setErrorHandler($error_handler);
$d = $DOM->parse($file);
XML::Xerces::error($@) if $@;
$doc = $DOM->getDocument;
$root = $doc->getDocumentElement;
$filter = MyNodeFilter->new;
$what = $XML::Xerces:
$walker = $doc->createTreeWalker($root,$what,$filter,1);
XML::Xerces::error($@) if $@;
$node = $walker->getCurrentNode;
#$cd = $cn->getFirstChild;
if ($node->hasChildNodes) {
my $text;
foreach my $child ($node->getChildNodes) {
if ($child->getNodeType ne
$XML::Xerces:
print "sda";
print $child->getNodeValue;
}
# if ($child->isa('XML::Xerces:
# $k .= $child->getNodeValue;
# }
# if ($child->isa('XML::Xerces:
# print "asd";
# print $k;
# $new{text} = $k;
# }
# $v = $child-> getElementsByTagName($k);
# print $v;
#}
## if $child->isa('XML::Xerces:
}
}
# print $text;
# $return->{text} = $text
# if $text !~ /^\s*$/;
--------------------------------- lr.xml
<?xml version="1.0"?>
<room xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation='lr.xsd'> <roomref>ccccccccccccccc</roomref> <roomday>2004-06-30Z</roomday> <roomprice>229.00</roomprice> </room>