Parsing XML Tags Help

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ralphNOSPAM@primemail.com

    #1

    Parsing XML Tags Help

    Is there a function or otherwise some way to pull out the target text
    within an XML tag?

    For example, in the XML tag below, I want to pull out 'CALIFORNIA'.

    <txtNameUSState >CALIFORNIA</txtNameUSState>

  • NC

    #2
    Re: Parsing XML Tags Help

    ralphNOSPAM@pri memail.com wrote:[color=blue]
    >
    > Is there a function or otherwise some way to pull out the target text
    > within an XML tag?[/color]

    There are at least two PHP extensions that allow parsing XML:


    PHP is a popular general-purpose scripting language that powers everything from your blog to the most popular websites in the world.


    There's also the SimpleXML extension, but it requires PHP 5.
    [color=blue]
    > For example, in the XML tag below, I want to pull out 'CALIFORNIA'.
    >
    > <txtNameUSState >CALIFORNIA</txtNameUSState>[/color]

    If this is all you want, you can simply do

    $state = strip_tags('<tx tNameUSState>CA LIFORNIA</txtNameUSState> ');

    or use regular expressions.

    Cheers,
    NC

    Comment

    • ralphNOSPAM@primemail.com

      #3
      Re: Parsing XML Tags Help

      On 9 Oct 2005 12:03:56 -0700, "NC" <nc@iname.com > wrote:
      [color=blue]
      >ralphNOSPAM@pr imemail.com wrote:[color=green]
      >>
      >> Is there a function or otherwise some way to pull out the target text
      >> within an XML tag?[/color]
      >
      >There are at least two PHP extensions that allow parsing XML:
      >
      >http://www.php.net/XML
      >http://www.php.net/DOMXML
      >
      >There's also the SimpleXML extension, but it requires PHP 5.
      >[color=green]
      >> For example, in the XML tag below, I want to pull out 'CALIFORNIA'.
      >>
      >> <txtNameUSState >CALIFORNIA</txtNameUSState>[/color]
      >
      >If this is all you want, you can simply do
      >
      >$state = strip_tags('<tx tNameUSState>CA LIFORNIA</txtNameUSState> ');
      >
      >or use regular expressions.
      >
      >Cheers,
      >NC[/color]

      There are multiple tags I need to strip out of the xml image so your
      $state = example wouldn't work. Sorry, I was trying not to get too
      deep.

      I did read the php xml extension before I posted but I got lost.

      I was hoping there was a function that would pull out the text between
      an xml tag. If there is such a function in the php xml extensions I
      couldn't see it ir otherwise figure it out.

      Comment

      • Malcolm Dew-Jones

        #4
        Re: Parsing XML Tags Help

        ralphNOSPAM@pri memail.com wrote:
        : On 9 Oct 2005 12:03:56 -0700, "NC" <nc@iname.com > wrote:

        : >ralphNOSPAM@pr imemail.com wrote:
        : >>
        : >> Is there a function or otherwise some way to pull out the target text
        : >> within an XML tag?
        : >
        : >There are at least two PHP extensions that allow parsing XML:
        : >
        : >http://www.php.net/XML
        : >http://www.php.net/DOMXML
        : >
        : >There's also the SimpleXML extension, but it requires PHP 5.
        : >
        : >> For example, in the XML tag below, I want to pull out 'CALIFORNIA'.
        : >>
        : >> <txtNameUSState >CALIFORNIA</txtNameUSState>
        : >
        : >If this is all you want, you can simply do
        : >
        : >$state = strip_tags('<tx tNameUSState>CA LIFORNIA</txtNameUSState> ');
        : >
        : >or use regular expressions.
        : >
        : >Cheers,
        : >NC

        : There are multiple tags I need to strip out of the xml image so your
        : $state = example wouldn't work. Sorry, I was trying not to get too
        : deep.

        : I did read the php xml extension before I posted but I got lost.

        : I was hoping there was a function that would pull out the text between
        : an xml tag. If there is such a function in the php xml extensions I
        : couldn't see it ir otherwise figure it out.

        Something like (obviously untested)

        # define parser

        $parser = xml_parser_crea te();
        xml_set_element _handler($parse r, "startEleme nt", "endElement ");
        xml_set_charact er_data_handler ($parser, "characterData" );

        # define some variables used later

        $saving=0;
        $text='';

        # open the xml file, read some data, details not shown

        OPEN YOUR FILE
        $data = GET SOME DATA() ... fread etc ...

        # feed data to parser

        while ($data != '')
        {
        if (!xml_parse($pa rser, $data, ? READ THE API )
        {
        $err =
        sprintf( "XML error: %s, at line %d, column %d\n"
        , xml_error_strin g(xml_get_error _code($parser))
        , xml_get_current _line_number($p arser)
        , xml_get_current _column_number( $parser)+1
        );
        die($err);
        }
        $data = GET SOME DATA() ... fread etc ...
        }
        xml_parser_free ($parser);

        # When you get here, then parsing has finished. If you saved the text in
        # an array (below) then that array would be full of strings by now.

        print "All done!";

        #
        # the functions below get called to handle the data during the parsing
        #

        function startElement($p arser, $tagname, $attrs)
        {
        if ($tagname == "txtNameUSState ")
        {
        $saving++;
        }
        }

        function endElement($par ser, $tagname)
        {
        if ($tagname == "txtNameUSState ")
        {
        $saving--;
        }

        if ($saving==0)
        {
        # OR save $text in an array - not shown
        do_something_wi th_the_text( $text );


        # BUT either way, you must clear the text string
        # ready for the next tag

        $text='';
        }

        }

        function characterData($ parser, $data)
        {
        if ($saving > 0)
        {
        $text .= $data;
        }
        }

        # ----- END oF PROGRAM


        I show calling a function for each bit of text. Another thing to do would
        be to push each string into an array. After the while/parse loop is
        finsihed then you would have an array of strings.


        --

        This programmer available for rent.

        Comment

        • NC

          #5
          Re: Parsing XML Tags Help

          ralphNOSPAM@pri memail.com wrote:[color=blue]
          >[color=green][color=darkred]
          > > > in the XML tag below, I want to pull out 'CALIFORNIA'.
          > > >
          > > > <txtNameUSState >CALIFORNIA</txtNameUSState>[/color][/color]
          >
          > There are multiple tags I need to strip out of the xml image so your
          > $state = example wouldn't work.[/color]

          Well, then you need to use an XML extension of some sort. Let's say
          your XML is stored at http://www.example.com/file.xml. Then you can
          try something like this:

          $content = file_get_conten ts('http://www.example.com/file.xml');
          $p = xml_parser_crea te();
          xml_parse_into_ struct($p, $content, $vals, $index);
          xml_parser_free ($p);
          unset($content) ;
          foreach ($index['txtNameUSState '] as $key => $id) {
          $value = $vals[$id]['value'];
          // now $value contains the text inside <txtNameUSState > tags
          }

          Cheers,
          NC

          Comment

          Working...