i have a problem with getting content from a XML into a mysql database.
This is the code:
$objDOM = new DOMDocument('1.0', 'UTF-8');
$objDOM->load("something.xml"); $IAutnr = $objDOM->getElementsByTagName("Data");
Now, in a for loop:
for($i=$t;$i<=$max;$i++) {
$some= $objDOM->getElementsByTagName("some");
$something = $some->item($i)->nodeValue;
$some2 = $objDOM->getElementsByTagName("some2");
$something2 = $some2->item($i)->nodeValue;
Now put $something and $something2 into the database
}
Now, what happens is, that everything works perfectly fine until one of the Elements (some,some2…) does not exist within the tag “Data”. So what he does, is taking the element from the next “Data”-tag and this mixes all my data, so I have data in my database, that actually doesnt belong there. And so I have an all mixed up database.
I allready tried for several hours to change the XML manually by putting the missing tags inside, but with thousands of data records, it is not possible.
So I need to add something into my code, that will have the effect, that if the tag doesnt exist, just leave it and dont take the tag from the next “Data”-Tag.
I actually dont even understand why he is doing that, why is he just jumping into the next “Data”-tag?
Thank you very much for your help!
I’m only guessing here about the content of your XML structure, but I imagine it looks something like
If this is the case, you should be looping over the collection of
Dataelements in$IAutnr, egUnless you need some of the more advanced features of the DOM library, I’d recommend using SimpleXML.