xml parsing

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • lnatz

    #1

    xml parsing

    I need to parse a configuration file using xml. How do I retrieve the
    data between the elements? For example, if my configuration file was
    organized in the following manner:

    <configFile>
    <server_nameNY_ ONLINE </server_name>
    <databaseSchedu lers </database>
    <tableSched_Inf o </table>
    <user_columnuse rID </user_column>
    <delete_columnD eleted </delete_column>
    <delete_value 2 </delete_value>
    </configFile>

    And would the textContent() function be needed? I considered doing the
    following:

    $server = "server_nam e";
    my $parser = XML::LibXML->new();
    my $doc = $parser->parse_file($co nfigFile);
    my $docRoot = $doc->getDocumentEle ment();
    my @configs = $docRoot->getChildrenByT agName($server) ;
    $dbserver= @configs[0] ->textContent( );

    I don't think it is properly written though and I don't think it would
    work, but would that be something close to the proper way of parsing
    the configuration file?

    Thanks in advance,
    Natalie

  • Joseph Kesselman

    #2
    Re: xml parsing

    lnatz wrote:
    I need to parse a configuration file using xml. How do I retrieve the
    data between the elements?
    If you're parsing using SAX, that's returned as characters() events
    (possibly multiple events per contiguous chunk of text; forgetting to
    allow for that is the single most common SAX error).

    If you're parsing into a DOM, that's returned as Text node children of
    the Element node that encloses them.
    And would the textContent() function be needed?
    That funtion isn't a part of either of those two standard APIs, so I
    can't advise.

    --
    Joe Kesselman / Beware the fury of a patient man. -- John Dryden

    Comment

    • Jürgen Kahrs

      #3
      Re: xml parsing

      lnatz wrote:
      I need to parse a configuration file using xml. How do I retrieve the
      data between the elements? For example, if my configuration file was
      organized in the following manner:
      >
      <configFile>
      <server_nameNY_ ONLINE </server_name>
      <databaseSchedu lers </database>
      <tableSched_Inf o </table>
      <user_columnuse rID </user_column>
      <delete_columnD eleted </delete_column>
      <delete_value 2 </delete_value>
      </configFile>
      Why dont you put the data into attributes ?
      Something like

      <configFile servername="NY ONLINE" database="Sched ulers">
      ...
      </configFile>

      Comment

      • lnatz

        #4
        Re: xml parsing

        I did not want to put the data into attribute because I read that an
        attribute is meant to describe the data in the elements. It is not
        proper coding to make the whole configuration file an attribute like :
        <configFile servername="NY ONLINE" database="Sched ulers">
        ...
        </configFile>
        But to respond to Joseph Kesselman..I'm using SAX but I just don't
        understand which functions to use to extract the data. For example:
        <server_nameNY_ ONLINE </server_name>
        How can I get the "NY_ONLINE" ? Can I use character(), is that what you
        mean? And what do you mean by :
        (possibly multiple events per contiguous chunk of text; forgetting to
        allow for that is the single most common SAX error).
        >

        Comment

        • lnatz

          #5
          Re: xml parsing

          I did not want to put the data into attribute because I read that an
          attribute is meant to describe the data in the elements. It is not
          proper coding to make the whole configuration file an attribute like :
          <configFile servername="NY ONLINE" database="Sched ulers">
          ...
          </configFile>
          But to respond to Joseph Kesselman..I'm using SAX but I just don't
          understand which functions to use to extract the data. For example:
          <server_nameNY_ ONLINE </server_name>
          How can I get the "NY_ONLINE" ? Can I use character(), is that what you
          mean? And what do you mean by :
          (possibly multiple events per contiguous chunk of text; forgetting to
          allow for that is the single most common SAX error).
          >

          Comment

          • Jürgen Kahrs

            #6
            Re: xml parsing

            lnatz wrote:
            I did not want to put the data into attribute because I read that an
            attribute is meant to describe the data in the elements. It is not
            proper coding to make the whole configuration file an attribute like :
            >
            > <configFile servername="NY ONLINE" database="Sched ulers">
            > ...
            > </configFile>
            Who said that ? I have seen _many_ XML files
            where all information is in the attributes.
            But to respond to Joseph Kesselman..I'm using SAX but I just don't
            understand which functions to use to extract the data. For example:
            <server_nameNY_ ONLINE </server_name>
            Maybe you give an example telling us what you
            want to do with the data. Dont get stuck when
            you run into details that arent essential.

            Comment

            • Johannes Koch

              #7
              Re: xml parsing

              Joseph Kesselman wrote:
              lnatz wrote:
              >And would the textContent() function be needed?
              >
              That funtion isn't a part of either of those two standard APIs, so I
              can't advise.
              textContent is part of DOM 3 Core.
              --
              Johannes Koch
              Spem in alium nunquam habui praeter in te, Deus Israel.
              (Thomas Tallis, 40-part motet)

              Comment

              • lnatz

                #8
                Re: xml parsing

                I figured it all out using XML::Simple, but thank you very much for
                your help.

                Comment

                • Joseph Kesselman

                  #9
                  Re: xml parsing

                  >But to respond to Joseph Kesselman..I'm using SAX but I just don't
                  >understand which functions to use to extract the data. For example:
                  <server_nameNY_ ONLINE </server_name>
                  How can I get the "NY_ONLINE" ?
                  Any good SAX tutorial should show you exactly how to do this. For
                  example, you might want to read through




                  --
                  Joe Kesselman / Beware the fury of a patient man. -- John Dryden

                  Comment

                  • William Park

                    #10
                    Re: xml parsing

                    lnatz <nmrabinovich@g mail.comwrote:
                    I need to parse a configuration file using xml. How do I retrieve the
                    data between the elements? For example, if my configuration file was
                    organized in the following manner:
                    >
                    <configFile>
                    <server_nameNY_ ONLINE </server_name>
                    <databaseSchedu lers </database>
                    <tableSched_Inf o </table>
                    <user_columnuse rID </user_column>
                    <delete_columnD eleted </delete_column>
                    <delete_value 2 </delete_value>
                    </configFile>
                    >
                    And would the textContent() function be needed? I considered doing the
                    following:
                    >
                    $server = "server_nam e";
                    my $parser = XML::LibXML->new();
                    my $doc = $parser->parse_file($co nfigFile);
                    my $docRoot = $doc->getDocumentEle ment();
                    my @configs = $docRoot->getChildrenByT agName($server) ;
                    $dbserver= @configs[0] ->textContent( );
                    >
                    I don't think it is properly written though and I don't think it would
                    work, but would that be something close to the proper way of parsing
                    the configuration file?
                    Not sure what you want to do after extracting the "data", but simple
                    printing would go something like

                    func () # Usage: func text
                    {
                    case ${XML_TAG_STACK[1]} in
                    configFile) echo "${XML_TAG_STAC K[0]}={$1}" ;;
                    esac
                    }

                    expat -d func < file.xml

                    which produces

                    server_name={ NY_ONLINE }
                    database={ Schedulers }
                    table={ Sched_Info }
                    user_column={ userID }
                    delete_column={ Deleted }
                    delete_value={ 2 }

                    --
                    William Park <opengeometry@y ahoo.ca>, Toronto, Canada
                    ThinFlash: Linux thin-client on USB key (flash) drive

                    BashDiff: Super Bash shell
                    Compare the best free open source Software Development Software at SourceForge. Free, secure and fast Software Development Software downloads from the largest Open Source applications and software directory

                    Comment

                    Working...