[PHP] Re: extracting data from XML file to PHP variable ..

2004-03-31 Thread Jason Barnett
Kenn Murrah wrote:

Greetings.

I've been RTFM all morning and for the live of me can't figure out how
to relate the element name to its data, e.g. if the element name is
fullname and the data is John Doe' how do I achieve $fullname =
John Doe  I must not be understanding how xml_parse works, and
my searches have turned up nothing.
Perhaps you should consider how you want your data structured.  I'm not 
sure what you're doing exactly, but consider the following file:
root
  people
person
  fullnameJohn Doe/fullname
/person
  /people
  person
fullnameKen Murrah/fullname
  /person
  person
fullnameJason Barnett/fullname
  /person
/root

Do you see the problem?  By assigning the character data to the element 
tag each time, you would end up with $fullname = Jason Barnett.  Would 
you want this to be an array instead to grab all fullname tags?

Any help would be appreciated.

thanks.



function startElement($xml_parser, $name, $attributes) {

print(piEncountered Start Element For: /i$name\n);
}
function endElement($xml_parser, $name) {
print(piEncountered End Element For: i/$name\n);
}
function characterData($xml_parser, $data) {
if ($data != \n) {
$data = ereg_replace (\:, , $data);
print (piEncountered Character Data: /i$data\n);
}
}
function load_data($file) {
$fh = fopen($file, r) or die (pCOULD NOT OPEN FILE!);
$data = fread($fh, filesize($file));
return $data;
}
$xml_parser = xml_parser_create();
xml_set_element_handler ($xml_parser, startElement, endElement);
xml_set_character_data_handler($xml_parser, characterData);
xml_parse($xml_parser, load_data($file)) or die (pERROR PARSING XML!);
xml_parser_free($xml_parser) ;


If you're trying to keep track of attributes more directly you might try 
using the Dom functions instead.
PHP4: http://www.php.net/domxml
(recommended) PHP5: https://www.zend.com/php5/articles/php5-xmlphp.php

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: [PHP] Re: extracting data from XML file to PHP variable ..

2004-03-31 Thread Kenn Murrah
Jason:

Yes, I am aware of the problem.  However, for the very simple XML files 
that I'm being asked to process, it's not an issue -- each file has, for 
example, one and only one occurrence of fullname  So, while I 
realize that any solution I achieve for this project will have virtually 
no portability to any future project, it would help me out of a terrible 
mind in dealing with today's issue :-)

thanks.

kenn

Jason Barnett wrote:

Kenn Murrah wrote:

Greetings.

I've been RTFM all morning and for the live of me can't figure out how
to relate the element name to its data, e.g. if the element name is
fullname and the data is John Doe' how do I achieve $fullname =
John Doe  I must not be understanding how xml_parse works, and
my searches have turned up nothing.


Perhaps you should consider how you want your data structured.  I'm 
not sure what you're doing exactly, but consider the following file:
root
  people
person
  fullnameJohn Doe/fullname
/person
  /people
  person
fullnameKen Murrah/fullname
  /person
  person
fullnameJason Barnett/fullname
  /person
/root

Do you see the problem?  By assigning the character data to the 
element tag each time, you would end up with $fullname = Jason 
Barnett.  Would you want this to be an array instead to grab all 
fullname tags?

Any help would be appreciated.

thanks.



function startElement($xml_parser, $name, $attributes) {
print(piEncountered Start Element For: /i$name\n);
}
function endElement($xml_parser, $name) {
print(piEncountered End Element For: i/$name\n);
}
function characterData($xml_parser, $data) {
if ($data != \n) {
$data = ereg_replace (\:, , $data);
print (piEncountered Character Data: /i$data\n);
}
}
function load_data($file) {
$fh = fopen($file, r) or die (pCOULD NOT OPEN FILE!);
$data = fread($fh, filesize($file));
return $data;
}
$xml_parser = xml_parser_create();
xml_set_element_handler ($xml_parser, startElement, endElement);
xml_set_character_data_handler($xml_parser, characterData);
xml_parse($xml_parser, load_data($file)) or die (pERROR PARSING 
XML!);
xml_parser_free($xml_parser) ;


If you're trying to keep track of attributes more directly you might 
try using the Dom functions instead.
PHP4: http://www.php.net/domxml
(recommended) PHP5: https://www.zend.com/php5/articles/php5-xmlphp.php

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: [PHP] Re: extracting data from XML file to PHP variable ..

2004-03-31 Thread Jason Barnett
Kenn Murrah wrote:

Jason:

Yes, I am aware of the problem.  However, for the very simple XML files 
that I'm being asked to process, it's not an issue -- each file has, for 
example, one and only one occurrence of fullname  So, while I 
realize that any solution I achieve for this project will have virtually 
no portability to any future project, it would help me out of a terrible 
mind in dealing with today's issue :-)

thanks.

kenn

Well a solution might be the function xml_parse_into_struct()
http://www.php.net/xml_parse_into_struct
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: [PHP] Re: extracting data from XML file to PHP variable ..

2004-03-31 Thread Justin Patrin
Jason Barnett wrote:

Kenn Murrah wrote:

Jason:

Yes, I am aware of the problem.  However, for the very simple XML 
files that I'm being asked to process, it's not an issue -- each file 
has, for example, one and only one occurrence of fullname  So, 
while I realize that any solution I achieve for this project will have 
virtually no portability to any future project, it would help me out 
of a terrible mind in dealing with today's issue :-)

thanks.

kenn

Well a solution might be the function xml_parse_into_struct()
http://www.php.net/xml_parse_into_struct
Hmmm, that looks interesting.

On another note, I've made a simple parse based on PEAR's XML_Parser 
that parses an entire XML tree into associative arrays. Feel free to use it.

http://www.reversefold.com/~papercrane/XML_Parser_Assoc/Assoc.phps

--
paperCrane Justin Patrin
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: [PHP] Re: extracting data from XML file to PHP variable ..

2004-03-31 Thread Jason Barnett
Hmmm, that looks interesting.

On another note, I've made a simple parse based on PEAR's XML_Parser 
that parses an entire XML tree into associative arrays. Feel free to use 
it.

http://www.reversefold.com/~papercrane/XML_Parser_Assoc/Assoc.phps

I haven't used that class before, or else I probably would have 
suggested it instead of reinventing the wheel.  On another note: what's 
the word on XML in PHP5?  Do most of the XML implementations in PEAR 
plan to continue using a SAX based parser or switching to a new 
Dom/SimpleXML implementation?  Just curious, I'd been thinking about a 
little something I might want to whip up.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php