473,506 Members | 16,951 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

XML Parser

I am updating a xml parser developed by a third party. The xml below is
parsed by the system using the code that follows:

<Company>
- <section id="officers">
- <section id="secretary">
<section id="stakeholder id">15728499</section>
<section id="name">FRED BLOGGS</section>
</section>
- <section id="director" type="current">
<section id="stakeholder id">15728498</section>
<section id="name">JOHN SMITH</section>
</section>
</section>
- <section id="company identification">
<section id="name">ABC LIMITED</section>

$extract = parseXMLForLtd($xml, $type);
function parseXMLForLtd($xml="", $type) {
preg_match_all( "/\<section(.*?)\<\/section\>/s", $xml, $reportData);
$reportData[0] = preg_replace('/\n/i','',$reportData[0]);
$result = $reportData[0];
for($i=0; $i<count($result);$i++) {
$item = trim(stripslashes($result[$i]));

if(eregi('<section id="name">',$item) && eregi('<section id="company
identification">',$item)) {
$extract[name] = preg_replace('/([<][\/a-zA-Z0-9 ="-]+[>])/i', '',
$item);

However when I try to parse a the other field called "name" using:

else if(eregi('<section id="name">',$item) && eregi('<section
id="secretary">',$item)) {
$extract[secretary] = preg_replace('/([<][\/a-zA-Z0-9 ="-]+[>])/i',
'', $item);

It does not work. Could anyone please advise how I can extract "Fred
Bloggs"?

Thanks In Advance
Jan 4 '08 #1
1 1438

Richard Price <ne**@directroute.co.ukwrote in
<13*************@corp.supernews.com>:
I am updating a xml parser developed by a third party. The
xml below is parsed by the system using the code that
follows:

$extract = parseXMLForLtd($xml, $type);
function parseXMLForLtd($xml="", $type) {
preg_match_all( "/\<section(.*?)\<\/section\>/s",
$xml, $reportData); $reportData[0] =
preg_replace('/\n/i','',$reportData[0]); $result =
$reportData[0]; for($i=0; $i<count($result);$i++) {
$item = trim(stripslashes($result[$i]));

if(eregi('<section id="name">',$item) && eregi('<section
id="company
identification">',$item)) {
$extract[name] = preg_replace('/([<][\/a-zA-Z0-9
="-]+[>])/i', '',
$item);
OMG.
However when I try to parse a the other field called
"name" using:

else if(eregi('<section id="name">',$item) &&
eregi('<section id="secretary">',$item)) {
$extract[secretary] =
preg_replace('/([<][\/a-zA-Z0-9 ="-]+[>])/i',
'', $item);

It does not work. Could anyone please advise how I can
extract "Fred Bloggs"?
Parsing hierarchical markup languages using regexen is an
exercise in futility, if not worse.

<http://www.php.net/manual/en/ref.dom.php>

An XPath expression fetching the node you need would be:

/Company/section[@id='officers']/
section[@id='secretary']/section[@id='name']/text()

--
....also, I submit that we all must honourably commit seppuku
right now rather than serve the Dark Side by producing the
HTML 5 spec.
Jan 4 '08 #2

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

Similar topics

13
2264
by: Paulo Pinto | last post by:
Hi, does anyone know of a Python package that is able to load XML like the XML::Simple Perl package does? For those that don't know it, this package maps the XML file to a dictionary.
11
9578
by: Jean de Largentaye | last post by:
Hi, I need to parse a subset of C (a header file), and generate some unit tests for the functions listed in it. I thus need to parse the code, then rewrite function calls with wrong parameters....
1
5268
by: Karalius, Joseph | last post by:
Can anyone explain what is happening here? I haven't found any useful info on Google yet. Thanks in advance. mmagnet:/home/jkaralius/src/zopeplone/Python-2.3.5 # make gcc -pthread -c...
3
3106
by: Himanshu Garg | last post by:
Hello, I am trying to pinpoint an apparent bug in HTML::Parser. The encoding of the text seems to change incorrectly if the locale isn't set properly. However Parser.pm in the directory...
2
3208
by: Joel Hedlund | last post by:
Hi! I have a possibly dumb question about imports. I've written two python modules: parser.py ------------------------------------ class Parser(object): "my parser"...
5
2315
by: thewarden | last post by:
I've come into a situation where I require to have BBCode parsed, this includes the standard tags supported by PEAR package HTML_BBCodeParser and custom BBCode tags I've added myself. My problem...
28
16350
by: Marc Gravell | last post by:
In Linq, you can apparently get a meaningful body from and expression's .ToString(); random question - does anybody know if linq also includes a parser? It just seemed it might be a handy way to...
0
2212
by: UncleRic | last post by:
Environment: Mac OS X (10.4.10) on MacBook Pro I'm a Perl Neophyte. I've downloaded the XML::Parser module and am attempting to install it in my working directory (referenced via PERL5LIB env): ...
18
4690
by: Just Another Victim of the Ambient Morality | last post by:
Is pyparsing really a recursive descent parser? I ask this because there are grammars it can't parse that my recursive descent parser would parse, should I have written one. For instance: ...
0
1336
by: arvindkgs | last post by:
Iam using c lexer that is flex generated and a c++ parser that is bison generated. i have modified the parser to acccept only string input. I am calling the parser function yyparse in a loop and...
0
7218
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7103
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...
0
7307
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,...
0
7370
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...
0
7478
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4701
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...
0
3188
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...
0
1532
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
409
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.