parsing xml with php, children
Hello I successfully created my parser
Everything is working great except one thing since my xml is formated a little different and I am totally lost on how to assign variable to the children of <photos>. xml portion <item> <url /> <name /> - <photos> <photo>1020944_0.jpg</photo> <photo>1020944_1.jpg</photo> <photo>1020944_2.jpg</photo> </photos> <user_id /> </item>PHP code <? global $insideitem, $tag, $name, $photos, $user_id; global $count,$db; $db = mysql_connect("localhost", "user","pass"); mysql_select_db("db_name",$db); $result = mysql_query("SELECT user_id FROM table,$db); while ($myrow = mysql_fetch_array($result)){ $uid=$myrow['user_id']; $UN_ID[$uid]=$uid; } $count=1; $count2=1; function startElement($parser, $name, $attrs) { global $insideitem, $tag, $name, $photos, $user_id; if ($insideitem) { $tag = $name; } elseif($name == "ITEM"){ $insideitem = true; } } function endElement($parser, $name) { global $insideitem, $tag, $name, $photos, $user_id; global $count,$count2,$db,$UN_ID; if ($name == "ITEM") { if(!$UN_ID[$unique_id]){ $name=addslashes($name); $photo1=addslashes($photo); $photo2=addslashes($photo); $photo3=addslashes($photo); $photo4=addslashes($photo); $user_id=addslashes($category); $sql = "INSERT INTO table ( name, photo1, photo2, photo3, photo4, user_id ) VALUES ( '$name', '$photo', '$photo', '$photo', '$photo', '$user_id', )"; $resultupdate = mysql_query($sql); } $name=''; $photos=''; $user_id=''; } } function characterData($parser, $data) { global $insideitem, $tag, $name, $photos, $user_id; if ($insideitem) { switch ($tag) { case "NAME": $name .= $data; break; case "PHOTOS": $photos .= $data; break; case "USER_ID": $user_id .= $data; break; } } } $xml_parser = xml_parser_create(); xml_set_element_handler($xml_parser, "startElement", "endElement"); xml_set_character_data_handler($xml_parser, "characterData"); $fp = fopen("../myfile.xml","r") or die("Error reading RSS data."); while ($data = fread($fp, 4096)) // Parse each 4KB chunk with the XML parser created above xml_parse($xml_parser, $data, feof($fp)) // Handle errors in parsing or die(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($xml_parser)), xml_get_current_line_number($xml_parser))); fclose($fp); xml_parser_free($xml_parser); ?>The number of <photo> tags can range between 1-4. I have tried searching everywhere for info on how to do this and tried everything but I just cant get it. After several days of this giving me headaches I really hope some one can enlighten me.
2 Answers
I think parsing the XML data into a tree like structure would be helpful. I did this for you, look here:
<?php $data = \' <item> <url /> <name /> <photos> <photo>1020944_0.jpg</photo> <photo>1020944_1.jpg</photo> <photo>1020944_2.jpg</photo> </photos> <user_id /> </item>\'; $tree = array(); $stack = array( &$tree ); function startElement( $parser, $name, $attrs ) { global $stack; $t = &$stack[ count( $stack ) - 1 ]; $t[ $name ][] = array(); $n = count( $t[ $name ] ); array_push( $stack, &$t[ $name ][ $n - 1 ]); } function endElement( $parser, $name ) { global $stack; array_pop( $stack ); } function characterData( $parser, $data ) { global $stack; $t = &$stack[ count( $stack ) - 1 ]; $t[ \'#CDATA\' ] = trim( $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, $data, TRUE ); foreach( $tree[ \'ITEM\' ][ 0 ][ \'PHOTOS\' ][ 0 ][ \'PHOTO\' ] as $photo ) { echo \"{$photo[ \'#CDATA\' ]}\\n\"; } ?>In fact, I have (amateurish) reimplemented what SimpleXML already gives you: <?php $xml = new SimpleXMLElement( $data ); foreach( $xml->photos->photo as $photo ) { echo \"$photon\"; } ?> Posted: MacOS 1 of 1 people found this answer helpful. Did you? Yes No
I'm not sure what your code is supposed to do and what your question is. You want to read the given XML structure and store the equivalent information in a database table, right? I noticed that you concatenate the character data in the string $photos, but never use that later. Is that what your problem is about?
Posted: MacOS 0 of 0 people found this answer helpful. Did you? Yes No |
© Advanced Web Core. All rights reserved