How my XML node structure works, is I have multiple children elements of parents that also occur multiple times. I am able to get to the children elements, but only the first one of each parent. I am also trying to insert the data in the children elements into MySQL. I just can’t seem to access any child element after the first one. When I echo count(), I’m able to get a total count of the children elements for each reoccurring parent, but I can’t seem to work with that data. Parsing XML is fairly new to me, and my PHP is a bit rusty, so please forgive me. What am I not getting?
Note: The data I’m concerned with is in the mt nodes. The immediate parent of the mt nodes is the mi node.
$string = file_get_contents('file1.xml');
$xml = new SimpleXMLElement($string);
foreach ($xml->md as $mt_tag) {
if (isset($mt_tag->mi->mt)) {
$query = "INSERT INTO xmlData(
xml_data
) VALUES (
'{$mt_tag->mi->mt}'
)";
mysql_query($query, $connection);
echo count($mt_tag->mi->mt);
echo "<br />";
} else {
echo "<p>Creation Failed.<p>";
echo "<p>" . mysql_error() . "</p>";
}
}
Youre missing a for loop on the mt nodes:
Please make sure to change this which is inconsistent
to