473,748 Members | 4,067 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2658
NC
ra*********@pri memail.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

Oct 9 '05 #2
On 9 Oct 2005 12:03:56 -0700, "NC" <nc@iname.com > wrote:
ra*********@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.

Oct 10 '05 #3
ra*********@pri memail.com wrote:
: On 9 Oct 2005 12:03:56 -0700, "NC" <nc@iname.com > wrote:

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

Oct 10 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
6488
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 void characters(char ch, int start, int length) throws SAXException{ String s = new String(ch, start, length); Compnames = s; }
2
2312
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 different tags that have to be converted to a HTML expression. E.g. "<pic#>" ( # is an integer number) has to be converted to <img src="..."> where the image data comes also from a database table. Since strings are immutable, is there an...
4
2308
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 SAX wouldn't do the parsing. What's wrong with this string (between <audit> and </audit> tags)? I am using SAX/Xerces 2.3.0 on Sun 8. Thanks in advance of any help. Nick Roman <?xml version="1.0" encoding="UTF-8"?> <lyr3:L3Transaction...
0
1560
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 me weird results. XML: /www.xmlspy.com) by AB(Co) --><!--Sample XML file generated by XML Spy v4.0.1 U (http://www.xmlspy.com)--> <RoomResRQ xmlns:commonelements="http://*.com/schema/CommonElements"
5
1963
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 into a buffer of an appropiate size, then searching for hardcoded (begin/end) tags and then parsing the data that way (through pointers) . Instead of hardcoding, im looking for a more abstract means to do this. So if any one knows of a library i...
1
2427
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, outlook, etc) - and i want to create some rules of how to get them out of the mail's html body. so at first i tried using regular expressions: for example - "any pattern that starts with a <p> and contains "from:"..." etc.
17
2790
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 might be a neat way to solve this, but I am new to them. Can anyone give me a hint here? The catch is, it must only find tokens that are not quoted and not commented; examples follow
9
4061
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" portions of Babe Ruth page (http://www.baseballprospectus.com/dt/ruthba01.shtml) and store that info in a CSV file. Also, I would like to do this for numerous players whose IDs I have stored in a text file (e.g.: cobbty01, ruthba01, speaktr01, etc.)....
5
2333
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 out the dates found. The condition is that multiple submissions by the same person on the same date is counted as 1. I have already gotten rid of the HTML tags using: #!/usr/bin/perl -w use strict;
0
8830
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9544
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9372
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8243
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6796
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6074
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4606
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4874
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2783
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.