Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

XML::Parser and Invalid XML

by Flame (Deacon)
on Jul 17, 2002 at 21:56 UTC ( #182599=perlquestion: print w/replies, xml ) Need Help??

Flame has asked for the wisdom of the Perl Monks concerning the following question:

I've got a bit of a problem with XML::Simple, which uses XML::Parser. I have a bit of xml that uses numbers as elements.

Ie:
<settings> <1>Your Name</1> <2>His Name</2> <3>324324324</3> </settings>


The problem is, no XML statement can begin with a number... so does anyone know how I can get XML::Parser to let that slide? Or does anyone know of something similar to XML::Simple which doesn't have such strict guidelines?

Thanks



"Weird things happen, get used to it."

Flame ~ Lead Programmer: GMS

Replies are listed 'Best First'.
•Re: XML::Parser and Invalid XML
by merlyn (Sage) on Jul 17, 2002 at 22:05 UTC
    I have a bit of xml that uses numbers as elements.
    No you don't. If it looks like that, it is not XML. It's a markup language of your own creation, unsupported by the world.

    If you change your DTD to something that permits this:

    <settings> <node item="1">Your Name</node> <node item="2">His Name</node> <node item="3">324324324</node> </settings>
    I think you'll be a lot happier, because then you can use the thousands of tools meant to be used with XML.

    -- Randal L. Schwartz, Perl hacker

      The proper format might even be closer to:

      <settings> <your_name>Your Name</your_name> <his_name>His Name</his_name> <number>324324324</number> </settings>

      XML markup should be descriptive. 1, 2 and 3, whether in tag names or as the only piece of information in the node tags are quite useless. And Perl will be just as happy to go through the keys of a hash than to the indexes in an array (BTW this comment is not directed to you merlyn ;--)

      --
      The Error Message is GOD - MJD

        I think I see part of the problem... the thing is, both the key and the value has meaning. In this case, I have a list of DID (Division ID) =< Division Name and in this file, DID => UID (Unique Member ID) and the reverse UID => DID to show what divisions a specific member is in. I'm just trying to store it. I've been using XML::Simple for a while now on other types of data, but it falls apart when the key is a number... :(



        "Weird things happen, get used to it."

        Flame ~ Lead Programmer: GMS

      As I said, I know it's breaking the rules, so it's technically not W3C XML, but it's what XML::Simple end's up outputting and calling XML, so I don't know what to do about it.

      I have a data structure like this:
      { 'uid' => { '1' => ['3','4','5','6'], '2' => ['4','3','1','5'], }, 'did' => { '1' => '2', '3' => ['1','2'], '4' => ['1','2'], '5' => ['1','2'], '6' => '1', }, }

      When I run XMLout() from XML::Simple on that structure, I get:
      <opt> <did 1="2" 6="1"> <3>1</3> <3>2</3> <4>1</4> <4>2</4> <5>1</5> <5>2</5> </did> <uid> <1>3</1> <1>4</1> <1>5</1> <1>6</1> <2>4</2> <2>3</2> <2>1</2> <2>5</2> </uid> </opt>


      So how can I fix it? I'm open to just about anything here including alternative storage systems, but XMLin() won't read what XMLout() outputs. Is there a way to trick XML::Parser (or expat or whatever it's called) into thinking <1>234</1> is valid?



      "Weird things happen, get used to it."

      Flame ~ Lead Programmer: GMS

        I think you are using the wrong tool here. If you really want to dump this structure as XML you could use Data::DumpXML (and its friend Data::DumpXML::Parser).

        --
        The Error Message is GOD - MJD

        This could be better, but you get the idea, just copy the hash with a tacked on token to all the hash keys.
        my %hash = qw(1 a 2 b 3 c); my $fixed_href = {}; fix_hash(\%fix_hash, $fixed_href); sub fix_hash { my ($old, $new) = @_; while (my ($key, $value) = each %$old) { (my $nkey = $key) =~ s/^(?=\d)/N/; if (ref($value) eq 'HASH') { $new_value = {}; $new->{$nkey} = $new_value; fix_hash($value, $new_value; } else { $new->{$nkey} = $value; } } }
Re: XML::Parser and Invalid XML
by mfriedman (Monk) on Jul 17, 2002 at 22:45 UTC
    The problem is, no XML statement can begin with a number... so does anyone know how I can get XML::Parser to let that slide?

    XML::Parser will not let it slide, because that is not valid XML. XML::Parser (expat, really) conforms extremely tightly to the XML standard. You need to get your data source to fix it, or if that's not possible perhaps run some regexes on the XML file before running through the parser.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2021-07-30 20:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?