Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

XML::Parser

by ivey (Beadle)
on Jun 20, 2000 at 23:30 UTC ( #19081=perlquestion: print w/ replies, xml ) Need Help??
ivey has asked for the wisdom of the Perl Monks concerning the following question:

Does anyone have experience with XML::Parser? I have a little chunk of xml that looks like this:
<part name="foo"> <part name="bar"/> <part name="bar2"/> </part>
and what i want to do is use this to initialize some objects:
my $foo = new Part(name => "foo"); $foo->addPart("bar",new Part(name => "bar")); $foo->addPart("bar2",new Part(name => "bar2")); $foo->startSystem;
I'm having some trouble with the Handlers to make that happen...so much so that I don't even have something that sorta works. I can't seem to get my head around it. Any suggestions?

Comment on XML::Parser
Select or Download Code
RE: XML::Parser
by Adam (Vicar) on Jun 20, 2000 at 23:38 UTC
    I'm wondering about your example.
    Your code snipit:
    <part name="foo"> <part name="bar"/> <part name="bar2"/> </part>
    is not valid xml. I know its psuedo-code, but it makes the sol'n confusing. Perhaps you meant:
    <section name="foo"> <subsection name="bar"> <subsection name="baz"> </section>
    Or something like that? Then you want
    my $foo = new section( name => "foo" ; $foo->addSubsection( "bar" ); $foo->addSubsection( "baz" ); $foo->startSystem;
    ???
      well...no. the difference is that i don't want to define a name for each level...there may be many levels. so, i might have
      <section name="top"> <section name="chap1"/> <section name="chap2"> <section name="sub1"/> </section> <section name="chap3"/> <section name="chap4"> <section name="sub2"> <section name="table1"/> </section> </section> </section>
      my problem is, in the Handlers definition, how keep a relationship between my "current root section" (ie, the one to which i will be adding the next subsection) and the xml level?

      --
      michael d. ivey, ivey@gweezlebur.com

        I didn't know you could do that. I've not done much with XML.
        You can keep a stack of the current level where you are in the XML file. When you get a start tag, push the tag onto the stack; when you get to the end of that tag, pop the tag off the stack. You can use this to define a tree structure of sorts.

        Another option would be to use the "Tree" Style of XML::Parser. Read the docs for that.

        A final option, and a good one, is to use XML::Simple, which may work for you:

        use XML::Simple; XMLin("foo.xml");
        It loads the data into a tree structure, which may or may not work exactly how you want it.

        Or (finally again) you could look into some of the other tree-processing XML modules, like XML::DOM and XML::Twig. They may be overkill for your purposes, though.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://19081]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (9)
As of 2014-11-23 19:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (134 votes), past polls