Re: Final questions with Xerces Perl
[email protected] (Jason E. Stewart)
| Newsgroups | gmane.text.xml.xerces-p.devel |
|---|---|
| Message-ID | <[email protected]> |
"Stephen Christensen" <[email protected]> writes: > Thanks for your help on my last question about Xerces Perl. I was > able to get a great sample someone on the mailing list that really > helped me figure out how to use Xerces. Cool - lists are a great help. > First, I have some elements that look like this: > <value name="foo"> > > I am currently calling $element->getNodeName(), which works great, and > returns the string "value". However, I cannot figure out how to > return the [ name="foo" ] part of the element. my $name = $element->getAttribute('name'); > My understanding is that this is the attribute of the node... but is > that correct? And if so, how do I retrieve that string? > getAttributes returns a DOMNamedNodeMap, so that isn't correct, > right? See the README, section 8.3) # returns a reference to a XML::Xerces::DOM_NamedNodeMap my $attr_map_ref = $element_node->getAttributes(); # returns a hash of the attributes my %attrs = $element_node->getAttributes(); put the method in scalar context and you get an object, put it in list context and you get a Perl hash - I prefer the Perl hash. > Secondly, the XML I am parsing has a lot of linebreaks in it. When > I try to parse it, it attempts to read those linebreaks as elements. > Is there a way to removewhitespace, or something of that nature? Two answers: 1) $node->getNodeType eq $XML::Xerces::DOMNode::ELEMENT_NODE; filter on the node type and throw away text nodes 2) if your XML file has a DTD you can can set a parser attribute to ignore optional whitespace: my $parser = XML::Xerces::AbstractDOMParser->new(); $parser->setIncludeIgnorableWhitespace(0); now you will not get the newlines, but this only works if your XML files use a DTD. Cheers, jas.