in reply to Re: XML::Simple--dealing with a variable element
in thread XML::Simple--dealing with a variable element

I'll crack the book to decipher your code. I simply need the names.

My end goal for all of this is to figure out existing attribute names being used, give a user the opportunity to select from that existing list or add new ones, write out a new <dataschema> stanza, and then update the config file where the <dataschemas> stanza came from.
  • Comment on Re^2: XML::Simple--dealing with a variable element

Replies are listed 'Best First'.
Re^3: XML::Simple--dealing with a variable element
by rhesa (Vicar) on Jul 26, 2006 at 23:01 UTC
    Are you sure the names are unique for all the varying groups? If so, then
    @attrs = map { keys %{ $_->{attributes}{attribute} } } values %{ $data->{dataschemas}{dataschema} };
    should do the trick. I think. I usually get dizzy with data structures this deep ;)
      Let's say for a given <dataschema> that one of the <attribute> stanza attributes is 'category'. Within that given <dataschema>, all 'category' names are unique. Some of those 'category' names, however, will appear in other <dataschema> stanza's 'category' names.

      Does that make sense? Does that change the code at all? Have I made you so dizzy that you can't get off the floor?
        I think I'd need to see more of your data to answer that conclusively. I wrote up a little test script (if only to keep my own sanity, hehe), and that showed me I did get the attribute names according to how I understand your data. Let me know where your real stuff differs.
        use XML::Simple; $data = XMLin( \*DATA, KeepRoot => 1 ); @attrs = map { keys %{ $_->{attributes}{attribute} } } values %{ $data->{dataschemas}{dataschema} }; print "@attrs"; __DATA__ <dataschemas> <dataschema name="varyingName1"> <attributes> <attribute> <one>1</one> <two>2</two> </attribute> </attributes> </dataschema> <dataschema name="varyingName2"> <attributes> <attribute> <three>3</three> <four>4</four> </attribute> </attributes> </dataschema> </dataschemas>

        Prints: one two three four