perl, XML::Simple, hashes

Discussion in 'XML' started by Steven M. O'Neill, Aug 30, 2007.

  1. I have an xml structure like this:

    <Meta name="fieldAttributes">
    <MetaString name="name">SUB_PHONE</MetaString>
    <MetaString name="value">999999999</MetaString>
    </Meta>

    <Meta name="fieldAttributes">
    <MetaString name="name">SUB_NAME</MetaString>
    <MetaString name="value">XML PLUS</MetaString>
    </Meta>

    I'm trying to parse it into a perl hash using XML::Simple
    such that I can access data like:

    print [something]->{SUB_PHONE} ;

    and get back "9999999999";

    (and likewise with the SUB_NAME, etc.)

    Anyone have an idea on how to get th

    (Not sure what this process is called, so I can't search Google
    effectively -- any clue would be super.)

    --
    Steven O'Neill
    Brooklyn, NY
    Is it Friday yet?
    http://panix.com/~steveo/cgi/is-it-friday-yet.html
    Steven M. O'Neill, Aug 30, 2007
    #1
    1. Advertising

  2. Steven M. O'Neill schrieb:
    > I have an xml structure like this:
    >
    > <Meta name="fieldAttributes">
    > <MetaString name="name">SUB_PHONE</MetaString>
    > <MetaString name="value">999999999</MetaString>
    > </Meta>
    >
    > <Meta name="fieldAttributes">
    > <MetaString name="name">SUB_NAME</MetaString>
    > <MetaString name="value">XML PLUS</MetaString>
    > </Meta>
    >
    > I'm trying to parse it into a perl hash using XML::Simple
    > such that I can access data like:
    >
    > print [something]->{SUB_PHONE} ;
    >
    > and get back "9999999999";


    Just look at the XML::Simple docs:
    http://search.cpan.org/~grantm/XML-Simple-2.18/lib/XML/Simple.pm#ValueAttr_=>_[_names_]_#_in_-_handy

    --
    Thomas Wittek
    Web: http://gedankenkonstrukt.de/
    Jabber: -pobox.net
    GPG: 0xF534E231
    Thomas Wittek, Aug 30, 2007
    #2
    1. Advertising

  3. Thomas Wittek <> wrote:
    >Steven M. O'Neill schrieb:
    >> I have an xml structure like this:
    >>
    >> <Meta name="fieldAttributes">
    >> <MetaString name="name">SUB_PHONE</MetaString>
    >> <MetaString name="value">999999999</MetaString>
    >> </Meta>
    >>
    >> <Meta name="fieldAttributes">
    >> <MetaString name="name">SUB_NAME</MetaString>
    >> <MetaString name="value">XML PLUS</MetaString>
    >> </Meta>
    >>
    >> I'm trying to parse it into a perl hash using XML::Simple
    >> such that I can access data like:
    >>
    >> print [something]->{SUB_PHONE} ;
    >>
    >> and get back "9999999999";

    >
    >Just look at the XML::Simple docs:
    >http://search.cpan.org/~grantm/XML-Simple-2.18/lib/XML/Simple.pm#ValueAttr_=>_[_names_]_#_in_-_handy


    I'm sorry, I should have mentioned that I've been reading the
    XML::Simple docs for days. Can you you give me more more clue?
    I still can't see how that section of the docs is analogous to
    my situation.

    --
    Steven O'Neill
    Brooklyn, NY http://www.panix.com/~steveo
    Steven M. O'Neill, Aug 30, 2007
    #3
  4. Steven M. O'Neill schrieb:
    > Thomas Wittek <> wrote:
    >> Steven M. O'Neill schrieb:
    >>> <Meta name="fieldAttributes">
    >>> <MetaString name="name">SUB_PHONE</MetaString>
    >>> <MetaString name="value">999999999</MetaString>
    >>> </Meta>
    >>> [..]
    >>> print [something]->{SUB_PHONE} ;
    >>> and get back "9999999999";

    >
    > I still can't see how that section of the docs is analogous to
    > my situation.


    Oh sorry, you're right. The cases don't match as you're doing the
    mapping on the content value instead of the attribute values.

    As I don't see something like a "ValueContent" setting, you probably
    have to do it manually.
    Although it should be relatively easy to transform (map) the hash to a
    hash that suits your needs.

    Maybe you can get a good answer in a Perl group.

    --
    Thomas Wittek
    Web: http://gedankenkonstrukt.de/
    Jabber: -pobox.net
    GPG: 0xF534E231
    Thomas Wittek, Aug 31, 2007
    #4
  5. Thomas Wittek <> wrote:
    >Steven M. O'Neill schrieb:
    >> I still can't see how that section of the docs is analogous to
    >> my situation.

    >
    >Oh sorry, you're right. The cases don't match as you're doing the
    >mapping on the content value instead of the attribute values.
    >
    >As I don't see something like a "ValueContent" setting, you probably
    >have to do it manually.
    >Although it should be relatively easy to transform (map) the hash to a
    >hash that suits your needs.


    Yeah, thanks. I ended up reading in the XML and then changing
    it into something else before running XML::Simple on it. Which
    may or may not be the best solution. It sure feels hackish,
    especially when I look at the regex it takes to do it :|

    --
    Steven O'Neill
    Brooklyn, NY http://www.panix.com/~steveo
    Steven M. O'Neill, Aug 31, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben Holness

    Hashes of Hashes via subs

    Ben Holness, Oct 5, 2003, in forum: Perl
    Replies:
    8
    Views:
    561
    Ben Holness
    Oct 8, 2003
  2. Steven Arnold

    using hashes as keys in hashes

    Steven Arnold, Nov 23, 2005, in forum: Ruby
    Replies:
    3
    Views:
    160
    Mauricio Fernández
    Nov 23, 2005
  3. kazaam
    Replies:
    12
    Views:
    271
    Matthias Wächter
    Sep 13, 2007
  4. Neela megha shyam Chivukula

    On Hashes - How the hashes printing works?

    Neela megha shyam Chivukula, May 27, 2009, in forum: Ruby
    Replies:
    4
    Views:
    238
    Markus Schirp
    May 28, 2009
  5. Tim O'Donovan

    Hash of hashes, of hashes, of arrays of hashes

    Tim O'Donovan, Oct 27, 2005, in forum: Perl Misc
    Replies:
    5
    Views:
    211
Loading...

Share This Page