You are here: Re: Parsing XML Tags Help « PHP Programming Language « IT news, forums, messages
Re: Parsing XML Tags Help

Posted by Malcolm Dew-Jones on 10/10/05 20:48

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

: >ralphNOSPAM@primemail.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('<txtNameUSState>CALIFORNIA</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_create();
xml_set_element_handler($parser, "startElement", "endElement");
xml_set_character_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($parser, $data, ? READ THE API )
{
$err =
sprintf( "XML error: %s, at line %d, column %d\n"
, xml_error_string(xml_get_error_code($parser))
, xml_get_current_line_number($parser)
, 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($parser, $tagname, $attrs)
{
if ($tagname == "txtNameUSState")
{
$saving++;
}
}

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

if ($saving==0)
{
# OR save $text in an array - not shown
do_something_with_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.

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация