Friday 15 March 2013

perl - Include parent node using XML::Parser -


I am trying to modify a Perl script that I found online which uses XML :: Parser Which identifies specific elements of XML documents and many times that each element contains a Pearl script and its documentation can be found:

  Use strict; Use warnings; Use XML :: Parser; File :: Use Search; @rgv or die "Usage: xmlales dir [dir ...] \ n"; My% element_count; My $ parser = xml :: parser-> new (handlers = & gt; {start = & gt; & amp; start_element;}); Search & amp; Process_xml, @ARGV; Print "$ _ ($ element_count {$ _}) \ n"% element_count for the ordered key; Go out; Sub process_xml {$ parser-> parsefile ($ _) if substr ($ _, -4) eq '.xml' and -f; } Sub start_element {my ($ expat, $ element, @attrval) = @_; $ Element_count {$ element} ++; }   

Generates an output like this:

  account (15614) account_NO (15504) active (15614) activity (6658) address (280 98)   

My question is how can I include the original node in the output?

The name of the parent node is given by current_element , such that < Code> $ name = $ expat- & gt; Current_element '/' $ Element and voilÃ! If you want to avoid warnings for empty parents' names, then my $ parent = $ expat- & gt; Current_element || ''; $ Name = "$ parent / $ element";

Then the handler gets this:

  sub start_element {my ($ expat, $ element, @ Attrval) = @_; My $ root = $ expat- & gt; Current_emment || ''; My $ name = "$ parent / $ element"; $ Element_count {$ name} ++; }    

No comments:

Post a Comment