parsing xml with php, children
Posted
by moustafa
on Stack Overflow
See other posts from Stack Overflow
or by moustafa
Published on 2010-06-07T07:47:05Z
Indexed on
2010/06/07
7:52 UTC
Read the original article
Hit count: 147
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 .
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;
// ##########################################################
// ************* START ELEMENT FUNCTION *********************
// ##########################################################
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);
// ##########################################################
// *********************** FREE MEMORY **********************
// ##########################################################
xml_parser_free($xml_parser);
?>
The number of 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.
© Stack Overflow or respective owner