Hello, I’m completely new to XML. Been reading up on simpleXML and am having trouble parsing a file because the elements I need seem to be at the same level.
I need to get the string under source (“Add Book…”) and the string under target (“Añadir libro…”) then put these into a table. The mysql part I can do no problem, but can’t figure out how to get them from the XML. I was using something like this:
$xml = new SimpleXMLElement($xmlstr);
foreach ($xml->xpath('//dict') as $dict) {
echo $dict->key, ' <strong> content: </strong> ', $dict->string, '<br />';
}
But that only gives me the first key and string objects.
Which would mean that its slightly more code to get, say, the spanish for “Add Book…”, but it’d be easier to find the german for “Añadir libro” , or the english for “Buch hinzufügen”.
You could use XPath to query for the source and target values. Given the XML presented in the first post, the code could look like;
$xml = new SimpleXMLElement($xmlstr);
foreach ($xml->array->dict as $dict) {
$source = current($dict->xpath("./key[.='source']/following-sibling::string"));
$target = current($dict->xpath("./key[.='target']/following-sibling::string"));
echo "'{$source}' translates to '{$target}'\
";
}
Note that SimpleXMLElement::xpath() returns an array, so we just use the the function current() to get the first value (which assumes there is one!).
Jake is quite right that you could probably make life easier if re-arranging the XML document is an option. However, if not, then the above could provide a hint as to the right direction to move in.
Since you have this badly structured xml, the solution isn’t flexible.
I assumed that your xml is structured like this in case of multiple entries (if it’s different, then the code will have to be changed accordingly) (btw, I changed from spanish to italian, because testing gave me errors with the spanish characters):
$xml = new SimpleXMLElement('http://www.galleons.it/test4/test.xml', null, true);
for this:
$xmlG = new SimpleXMLElement('uploads/xmlTestFile.xml', null, true);
Which is the exact same file, just calling it from a local file I have, I get an “invalid argument for foreach()” error. It’s weird that the other code I have works fine with this file so there must be something strange I’m missing.
I was able to get more all the lines by adding plist/ to the path:
/plist/dict/array/dict
So I think that should do it!
(end edit)
Hmm… there’s something I’m missing. It works with your file, but it won’t work with mine, even if I use a relative or an http path. In fact print_r($result) doesn’t return anything at all.
Again, I don’t think it’s my document since the other code produces output.
Ok, we’re getting somewhere. If I copy your file into a local document, it works. But your code will not work with my file. The difference I see in the headers of both files are:
Hello again, I am getting the following errors when simpleXml finds characters with the umlaut on them, the 2 little dots used in German and Scandinavian languages over vowels: ü