Re: [PHP] XML Parsing, starting out.
[EMAIL PROTECTED] escribió: Hello, 1st msg here so im excited what results i will get :) Im a novice with php parsing and have been able to help myself quite well with what ive needed with xml_parse_into_struct and then echoing the fixed variable names but now im dealing with an xml document that is 1st quite large and 2nd changing gradually over time with either added data or changed information. (But not in large numbers) http://www.camelotherald.com/xml/spells-si.xml It is a spell listing for an online mmorpg. It has a "spell_list" that captures all other tags and a "spell_line" for each spell line. Within each spell line is a "spell" which has the detailed info for each spell with in the "spell_line" I could parse this by echoing the fixed variables but due to its size and the changes done over time its just to much work. This is why i come to a stop. Ive read some guides and tried some example code that should help me through this. What i hoped to get is an example with the xml i provided so i could work with it to complete the project. The example could look something like this: Header with the selected "spell_line" name ie. "Calefaction" and below that the spell info in some dummy table format. Then the next "spell_line" which is "Path of Earth" then with the spell info in some dummy table setup. Thanks for reading and i hope someone can show me the light of a real xml parse. Hi, Some months ago I was looking for some code for "loading" dynamic xml data structures my php scripts. I didn't found anything simple and flexible for my needs. I found handy the PEAR XML_beautifier tokenizer, and then I write a simple function to extract all the data with the right array structure as needed for my project. A recursive function to extract tag elements data from the tokenized arrays (attached below) Have a look at the code below. In some lines I process part of the contents from your xml sample file, so you can have something to get started, it you find handy my approach. I've attached a partial output from the data extracted by this code too. To run the code you need PEAR and XML_Beautifier and dependencies (if any, now don't remember if XML_parser is in the PEAR base install or not). Have fun, :-) Gonzalo Monzón http://www.camelotherald.com/xml/spells-si.xml";; $data = XML_tokenize($xmlsrc,True); $spell_list = _gxml_bds_extract_tag('spell_list','children',$data); // Extract spell_lines attribute data: $spell_lines_attr = _gxml_bds_extract_tag('spell_line','attribs',$spell_list,1,-1); // Extract spell_lines children data: $spell_lines_data = _gxml_bds_extract_tag('spell_line','children',$spell_list,1,-1); // Extract spell data and attributes from spell_lines: foreach($spell_lines_data as $k => $val) { $spells_attr[$k] = _gxml_bds_extract_tag('spell','attribs',$val,1,-1); $spells_data[$k] = _gxml_bds_extract_tag('spell','children',$val,1,-1); // Extract data from all spells in $k spell_line: foreach($spells_data[$k] as $k2 => $val2) { $tmp = _gxml_bds_extract_tag('level','children',$val2); $spells_data_val[$k][$k2]['level'] = $tmp[0]['data']; $tmp = _gxml_bds_extract_tag('damage_type','children',$val2); $spells_data_val[$k][$k2]['damage_type'] = $tmp[0]['data']; } } /* (A) ** */ echo $spell_lines_attr[0]['name'].""; foreach($spells_attr[0] as $k => $val) { echo $k.""; _print_bDS($val); _print_bDS($spells_data_val[0][$k]); echo ""; } /* (B) ** */ _print_bDS($spells_data[0][0]); /* ** Output: (A) ** Calefaction 0 name => Minor Shield of Magma desc => Creates a field that damages anyone who attacks the target in melee. id => 22 target => Realm level => 1 damage_type => Matter 1 name => Shield of Magma desc => Creates a field that damages anyone who attacks the target in melee. id => 23 target => Realm level => 5 damage_type => Matter 2 name => Greater Shield of Magma desc => Creates a field that damages anyone who attacks the target in melee. id => 24 target => Realm level => 9 damage_type => Matter ... ** Output: (B) ** 0 type => 1 data => depth => 3 1 type => 2 tagname => level attribs contains => 1 depth => 3 children 0 type => 1 data => 1 depth => 4 2 type => 1 data => depth => 3 3 type => 2 tagname => range attribs contains => 1 depth => 3 children 0 type => 1 data => 1000 depth => 4 4 type => 1 data => depth => 3 5 type => 2 tagname => damage attribs contains => 1 depth => 3 children 0 type => 1 data => 0.7 depth => 4 6 type => 1 data => depth => 3 7 type => 2 tagname => damage_type attribs contains => 1 depth => 3 children 0 type => 1 data => Matter depth => 4 ... */ function XML_tokenize($data,$file=False) { $p = &new XML_Beautifier_Tokenizer("iso-8859-1"); return $p->tokenize($data,$f
Re: [PHP] XML Parsing, starting out.
Mattias Thorslund wrote: [EMAIL PROTECTED] wrote: Hello, 1st msg here so im excited what results i will get :) Im a novice with php parsing and have been able to help myself quite well with what ive needed with xml_parse_into_struct and then echoing the fixed variable names but now im dealing with an xml document that is 1st quite large and 2nd changing gradually over time with either added data or changed information. (But not in large numbers) http://www.camelotherald.com/xml/spells-si.xml It is a spell listing for an online mmorpg. It has a "spell_list" that captures all other tags and a "spell_line" for each spell line. Within each spell line is a "spell" which has the detailed info for each spell with in the "spell_line" I could parse this by echoing the fixed variables but due to its size and the changes done over time its just to much work. This is why i come to a stop. Ive read some guides and tried some example code that should help me through this. What i hoped to get is an example with the xml i provided so i could work with it to complete the project. The example could look something like this: Header with the selected "spell_line" name ie. "Calefaction" and below that the spell info in some dummy table format. Then the next "spell_line" which is "Path of Earth" then with the spell info in some dummy table setup. Thanks for reading and i hope someone can show me the light of a real xml parse. The comments on the Manual page [http://www.php.net/manual/en/ref.xml.php] contain an example class that parses an XML file into an associative array. I'm using something similar myself, as it seems to be a pretty straightforward way to get an easy-to-use structure in PHP 4 without dom_xml, which isn't always available. Mattias I have been trying as i said above with the php.net examples, the sitepoint examples (which have been great to give me some view of what i need and how it functions basicly) but with no luck. I can make it easy with fixed value but the dynamic part of it is still outside my reach for now. Thanks for the examples Matt and Richard - olafurw -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] XML Parsing, starting out.
[EMAIL PROTECTED] wrote: > Hello, 1st msg here so im excited what results i will get :) > > Im a novice with php parsing and have been able to help myself quite well > with what ive needed with xml_parse_into_struct and then echoing the fixed > variable names but now im dealing with an xml document that is 1st quite > large and 2nd changing gradually over time with either added data or changed > information. (But not in large numbers) > > http://www.camelotherald.com/xml/spells-si.xml > > It is a spell listing for an online mmorpg. It has a "spell_list" that > captures all other tags and a "spell_line" for each spell line. Within each > spell line is a "spell" which has the detailed info for each spell with in > the "spell_line" > > I could parse this by echoing the fixed variables but due to its size and the > changes done over time its just to much work. This is why i come to a stop. > Ive read some guides and tried some example code that should help me through > this. > > What i hoped to get is an example with the xml i provided so i could work > with it to complete the project. > > The example could look something like this: > > Header with the selected "spell_line" name ie. "Calefaction" and below that > the spell info in some dummy table format. Then the next "spell_line" which > is "Path of Earth" then with the spell info in some dummy table setup. > > Thanks for reading and i hope someone can show me the light of a real xml > parse. > The comments on the Manual page [http://www.php.net/manual/en/ref.xml.php] contain an example class that parses an XML file into an associative array. I'm using something similar myself, as it seems to be a pretty straightforward way to get an easy-to-use structure in PHP 4 without dom_xml, which isn't always available. Mattias -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] XML Parsing, starting out.
On Sat, April 29, 2006 11:19 am, [EMAIL PROTECTED] wrote: > Im a novice with php parsing and have been able to help myself quite > well with what ive needed with xml_parse_into_struct and then echoing In an ideal world, you would examine other XML parsing options, such as the PEAR library. If, like me, the machine in question does not have, and is unlikely to have, anything BUT the expat library in the near future, a pointer to a function that uses expat and creates something less unwieldy than the xml_parseintostruct would be most welcome... So let me know if anybody responds off-list, please. I'll be Googling for it Monday morning myself. -- Like Music? http://l-i-e.com/artists.htm -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] XML Parsing, starting out.
Hello, 1st msg here so im excited what results i will get :) Im a novice with php parsing and have been able to help myself quite well with what ive needed with xml_parse_into_struct and then echoing the fixed variable names but now im dealing with an xml document that is 1st quite large and 2nd changing gradually over time with either added data or changed information. (But not in large numbers) http://www.camelotherald.com/xml/spells-si.xml It is a spell listing for an online mmorpg. It has a "spell_list" that captures all other tags and a "spell_line" for each spell line. Within each spell line is a "spell" which has the detailed info for each spell with in the "spell_line" I could parse this by echoing the fixed variables but due to its size and the changes done over time its just to much work. This is why i come to a stop. Ive read some guides and tried some example code that should help me through this. What i hoped to get is an example with the xml i provided so i could work with it to complete the project. The example could look something like this: Header with the selected "spell_line" name ie. "Calefaction" and below that the spell info in some dummy table format. Then the next "spell_line" which is "Path of Earth" then with the spell info in some dummy table setup. Thanks for reading and i hope someone can show me the light of a real xml parse. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php