472,992 Members | 3,164 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,992 software developers and data experts.

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>

Oct 9 '05 #1
4 2579
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Wes Batson | last post by:
Hi, I am using SAX parsing and I need to get the information between the tags. I have narrowed the problem down to my characters() method in the DefaultHandler class. Here is my code: public...
2
by: Peter Sprenger | last post by:
Hello, I hope somebody can help me with my problem. I am writing Zope python scripts that will do parsing on text for dynamic webpages: I am getting a text from an oracle database that contains...
4
by: silviu | last post by:
I have the following XML string that I want to parse using the SAX parser. If I remove the portion of the XML string between the <audit> and </audit> tags the SAX is parsing correctly. Otherwise...
0
by: Naren | last post by:
I have an XML like the one below. I am using SAX parsing and I need to get the information between the tags of the Email element. First i try to access the content and print it out and it gives...
5
by: minboymike | last post by:
Hello Everyone, I was wondering if anyone knows of a simple(r) way to parse an xml document. The current way I have been parsing an xml doc in my code has been reading the entire xml document...
1
by: yonido | last post by:
hello, my goal is to get patterns out of email files - say "message forwarding" patterns (message forwarded from: xx to: yy subject: zz) now lets say there are tons of these patterns (by gmail,...
17
by: Mark | last post by:
I must create a routine that finds tokens in small, arbitrary VB code snippets. For example, it might have to find all occurrences of {Formula} I was thinking that using regular expressions...
9
by: ankitdesai | last post by:
I would like to parse a couple of tables within an individual player's SHTML page. For example, I would like to get the "Actual Pitching Statistics" and the "Translated Pitching Statistics"...
5
by: moddster | last post by:
Hi Guys. I am a newbie to perl and need some help with a problem. PROBLEM: I have to parse an HTML file and get rid of all the HTML tags and count the number of sumbissions a person has through...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.