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

Re: Kindly suggest a good starting point for XML Parsing.

by pileofrogs (Priest)
on Dec 30, 2011 at 00:07 UTC ( #945572=note: print w/replies, xml ) Need Help??

in reply to Kindly suggest a good starting point for XML Parsing.

This might be a moot point, but if possible you might want to consider moving away from XML to something like YAML or JSON. I hate XML and I know a lot of other people do too.

Here's my problem with XML:
<foo name=bobby><name>robert</name></foo></p>

How do you represent that in data? How do you take the resulting data and make it turn back into XML that looks the same? I know you can, but there's no obvious 1 to 1 mapping. You basically have to make one up and you can avoid that whole problem with JSON or YAML.


  • Comment on Re: Kindly suggest a good starting point for XML Parsing.

Replies are listed 'Best First'.
Re^2: Kindly suggest a good starting point for XML Parsing.
by choroba (Archbishop) on Dec 30, 2011 at 00:45 UTC
    Can you be more specific? Are you talking about escaping control characters? Is your example correct?
    use XML::LibXML; $s = "&ltfoo name=bobby>&ltname&gtrobert</name></foo></p>"; print "before: $s.\n"; $dom = XML::LibXML->load_xml(string => "<root/>"); $dom->findnodes("/root")->[0]->appendText($s); print $dom->serialize; $s2 = $dom->findnodes("//text()")->[0]->data; print "after: $s2.\n";
    before: &ltfoo name=bobby>&ltname&gtrobert</name></foo></p>. <?xml version="1.0"?> <root>&amp;ltfoo name=bobby&gt;&amp;ltname&amp;gtrobert&lt;/name&gt;&l +t;/foo&gt;&lt;/p&gt;</root> after: &ltfoo name=bobby>&ltname&gtrobert</name></foo></p>.

      I probably should have put that in code tags instead of using all those &lt, &gt. Sorry.

      <foo name=bobby><name>robert</name></foo>

      My point is, your foo has two names.

      <foo name=bobby><age>27</age></foo>

      Also sucks. It's no different from

      <foo age=27><name>bobby</name></foo>

      Obviously it's different, but not in a truly meaningful way. You could just as easily say

      <foo age=27 name=bobby></foo>


        I got it. From this point of view, you are right. You did not mention
        <foo> <name>Robert</name> <name>Bobby</name> </foo>
        But you can view it inside out: data types suck, because they cannot easilly represent XML :-)
Re^2: Kindly suggest a good starting point for XML Parsing.
by tobyink (Canon) on Jan 04, 2012 at 00:44 UTC

    Meh. How do you represent this structure in JSON?

    my $a = []; my $b = { a => $a }; push @$a, $b; print to_json($a); # ???

    Real life needs you to be able to deal with structures like that. XML and JSON are both poor serialisations for real life data. YAML is somewhat better in this regard but has its own shortcomings.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://945572]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2021-01-21 08:15 GMT
Find Nodes?
    Voting Booth?