Parsing XML and storing attributes in MySQL using Perl

Discussion in 'Perl Misc' started by jkugler, Jun 12, 2006.

  1. jkugler

    jkugler Guest


    I am trying to store huge amounts of data from xml files and put them
    into a MySQL database. The xml files all are in this format:

    <?xml version="1.0" encoding="UTF-8"?>
    <snp_submission xsi:schemaLocation="
    <group lsid=""/>
    <name>Steve Sherry</name>

    <allele base="C"/>
    <allele base="T"/>

    <length_class value="full-length"/>
    <genomic_location active="true" type="exact">
    <coordinates chrom="11" start="60749" stop="60749"

    I am trying to extract the following information: lsid, allele base,
    cooordinates chrom, start, and strand. I would ideally like to use a
    perl script to pull this data out and put it in MySQL, but I have had
    no luck with XML::parser since the information I am looking for is
    embedded in the element's attributes.

    If anyone has any ideas on how to get this data out of xml and into
    MySQL, it would be much appreciated.

    Thanks so much,

    jkugler, Jun 12, 2006
    1. Advertisements

  2. jkugler

    robic0 Guest

    [xml snipped]

    Hi James.

    If you want you could use (ver .91) posted on this group.
    It would send the same attribute info to a tag handler.
    Should be the same as the one you used.
    From there you could check the "name" for the "value" data.

    If you could hang on a while, I'm writing filter interfaces for
    tags, namespace, attributes, content, etc.. The filters can be
    either regexp or dos style. Each filter is specific, set by the
    user who provides the handler to recieve that specific info.

    Whats nice is that the regexp provided by the user is applied to
    just the sub-parsed global data, the category is guaranteed.
    Tag is tag, attrib is attrib, content is content.

    Its almost there. Yours is a basic search but the intent is to
    provide search/replace as well as multiple filter capability.
    At my stage of development with this its really pretty easy.
    The hold up is a base compliant parser, which is almost done.

    Anyway, just some info for you. No need to use RXParse. The difference
    between my module and all thats out there is its whole purpose is meant
    to be interactive. Funny how the simple things you would expect from
    high power software just aint there, and its so easy to do!

    (god of porn)
    robic0, Jun 12, 2006
    1. Advertisements

  3. jkugler

    J. Gleixner Guest

    jkugler wrote:
    Use XML::parser to get the element's value, then parse the value using
    regular expressions, split, or various other means.

    What have you tried? No one can point out coding errors, unless you post
    your code.

    Look at using DBI, and DBD::mysql for interacting with MySQL. It's all
    available on CPAN ( ).
    J. Gleixner, Jun 12, 2006
  4. jkugler

    xhoster Guest

    What is huge? Is it the number of files, or the number of entries per
    file? If the former, I would probably use XML::Simple. If the later,
    maybe look into XML::Twig.

    lsid appears at more than one level.

    I haven't had any trouble getting element attributes from XML files with
    XML::parser. Can you show some of what you have tried so far?
    Here is something trivial I whipped up that just shows how to get the
    attributes for the allele:

    use strict;
    use Data::Dumper;
    use XML::parser;
    my $p1 = new XML::parser(Style =>'Subs');
    open my $fh, "test.xml" or die $!;

    sub allele {
    my ($expat,$ele,%att) = @_;
    print Dumper \%att

    $VAR1 = {
    'base' => 'C'
    $VAR1 = {
    'base' => 'T'

    no element found at line 73, column 0, byte 3851 at
    line 187

    (That error is only there because the sample file you provided was

    xhoster, Jun 12, 2006
  5. jkugler

    robic0 Guest

    No no! Attribute data is "content", why invoke Simple when its just content?
    The attribute is known, any tag handler will do.
    Don't make it into a federal case, <gods name=jeezz/>.
    robic0, Jun 12, 2006
  6. jkugler

    John Bokma Guest

    XML::parser can handle attributes perfectly. What exactly didn't work?
    XML::Twig? Give me a book? :)
    John Bokma, Jun 13, 2006
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.