How to Read xml without knowing its structure -


whether can read xml file without knowing strucutre. can perform kind of mapping between nodes.for example xml

<bookstore>   <location category="us">   <book category="cooking">     <title lang="en">everyday italian</title>     <author>xyz</author>     <year>2005</year>     <price>30.00</price>   </book>   <book category="sample">     <title lang="en">everyday italian</title>     <writer>abcd</writer>     <year>2005</year>     <price>30.00</price>   </book>  </location>      <location category="uk">   <book category="cooking">     <title lang="en">everyday italian</title>     <author>giada de laurentiis</author>     <year>2005</year>     <price>30.00</price>   </book>   <book category="sample">     <title lang="en">everyday italian</title>     <writer>giada de laurentiis</writer>     <year>2005</year>     <price>30.00</price>   </book>  </location> </bookstore> 

in sample xml, want read bookstore in location , authors.here authors , writers simillar want consider writer author.i want extract author names.(i.e) output xyz,abc. how can perform mapping.how can read without knowing structure.is there anyway this.is possible? first.sorry if dumb question.

it's not dumb question per-se, it's hard answer without more detail. xml structured data format.

however parsers - perls xml::twig - can things traverse , down nodes, , parse structure dynamically. - example - merge author , writer.

e.g. this:

use strict; use warnings; use xml::twig;  %books;  sub process_book {     ( $twig, $book ) = @_;     $book->print;     print "--\n";     $title = $book->first_child('title')->text;     if ( $books{$title} ) {         foreach $element ( $book->children ) {             if ( not $books{$title}->first_child( $element->tag ) ) {                 $new_element = $element->cut;                 $new_element->paste( $books{$title} );             }         }         $book->delete;     }     else {         $books{$title} = $book;     } }  $twig = xml::twig->new(     pretty_print  => 'indented',     twig_handlers => { 'book' => \&process_book, }, )->parse( \*data ); $twig->print;  __data__ <bookstore>   <location category="us">   <book category="cooking">     <title lang="en">everyday italian</title>     <author>xyz</author>     <year>2005</year>     <price>30.00</price>   </book>   <book category="sample">     <title lang="en">everyday italian</title>     <writer>abcd</writer>     <year>2005</year>     <price>30.00</price>   </book>  </location>      <location category="uk">   <book category="cooking">     <title lang="en">everyday italian</title>     <author>giada de laurentiis</author>     <year>2005</year>     <price>30.00</price>   </book>   <book category="sample">     <title lang="en">everyday italian</title>     <writer>giada de laurentiis</writer>     <year>2005</year>     <price>30.00</price>   </book>  </location> </bookstore> 

which run through books , merge them based on title. in example above, you'll get:

<bookstore>   <location category="us">     <book category="cooking">       <writer>abcd</writer>       <title lang="en">everyday italian</title>       <author>xyz</author>       <year>2005</year>       <price>30.00</price>     </book>   </location>   <location category="uk"></location> </bookstore> 

i can't more specific without bit more understanding you're trying accomplish, can quite clever things xml::twig.


Comments

Popular posts from this blog

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

php - render data via PDO::FETCH_FUNC vs loop -

The canvas has been tainted by cross-origin data in chrome only -