By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,570 Members | 1,251 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,570 IT Pros & Developers. It's quick & easy.

Parsing XML Tags Help

P: n/a
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>

Oct 9 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
NC
ra*********@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

Oct 9 '05 #2

P: n/a
On 9 Oct 2005 12:03:56 -0700, "NC" <nc@iname.com> wrote:
ra*********@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.

Oct 10 '05 #3

P: n/a
ra*********@primemail.com wrote:
: On 9 Oct 2005 12:03:56 -0700, "NC" <nc@iname.com> wrote:

: >ra*********@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.
Oct 10 '05 #4

P: n/a
NC
ra*********@primemail.com wrote:
in the XML tag below, I want to pull out 'CALIFORNIA'.

<txtNameUSState>CALIFORNIA</txtNameUSState>


There are multiple tags I need to strip out of the xml image so your
$state = example wouldn't work.


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_contents('http://www.example.com/file.xml');
$p = xml_parser_create();
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

Oct 10 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.