Using XML::libXML to parse xml--get nth Child Node

DynS

New Member
I have an xml document that contains information I want to put into a database. Example:\[code\]<pc> <name>John</name> <last>Smith</last> <address> <business> <street>987 Broad Way</street> <city>New York</city> <state>New York</state> </business> <home> <street>123 Main St.</street> <city>Jersey City</city> <state>Nebraska</state> </home> </address> <phone>123-456-7890</phone></pc>\[/code\]I am planing on using perl to parse this data to build mysql statements. I am having problems getting a format like the following:\[code\]name="John"last="Smith"...\[/code\]The code I am using so far is:\[code\]use strict;use warnings;use XML::LibXML;my $filename = "sample.xml";my $parser = XML::LibXML->new();my $xmldoc = $parser->parse_file($filename);for my $contact ($xmldoc->findnodes('/server')){ print $contact->nodeName(),"\n"; foreach my $child ($contact->findnodes('*')){ print "\t", $child->nodeName(), ":\n"; foreach my $grandchild ($child->findnodes('*')){ print"\t\t", $grandchild->nodeName(), ":", $grandchild->textContent(), "\n"; } }}\[/code\]I don't use perl much, and I am not overly familiar with the XML::libXML library (http://search.cpan.org/~shlomif/XML-LibXML-2.0014/LibXML.pod). I guess I need to find the last child and work my way back up the node? I am kind of lost as to how I should go about constructing my "inserts" and "updates" for mysql.
 
Top