Strahan Posted May 16, 2013 Share Posted May 16, 2013 This is making me crazy. I am trying to find data from AniDB.net. I have an XML file of all the titles, this is the XML: <?xml version="1.0" encoding="UTF-8"?> <animetitles> <anime aid="1"> <title type="short" xml:lang="en">CotS</title> <title type="official" xml:lang="en">Crest of the Stars</title> <title type="official" xml:lang="fr">Crest of the Stars</title> <title type="official" xml:lang="pl">Crest of the Stars</title> <title type="syn" xml:lang="cs">Hvězdný erb</title> <title type="main" xml:lang="x-jat">Seikai no Monshou</title> <title type="short" xml:lang="x-jat">SnM</title> <title type="syn" xml:lang="zh-Hans">星界之纹章</title> <title type="official" xml:lang="ja">星界の紋章</title> </anime> <anime aid="9887"> <title type="main" xml:lang="x-jat">Chihayafuru OAD</title> <title type="official" xml:lang="ja">ちはやふる OAD</title> </anime> </animetitles> <!-- Created: Thu May 16 02:00:33 2013 (7692 anime, 41979 titles) --> What I'm doing is pulling these animes and inserting into a MySQL database. I just need to get the main title and the official english titles. I have no problem iterating through the title tags and checking the type, but the namespaced "xml:lang" attribute is screwing me up. This is my code: $data = simplexml_load_file("anime-titles_2.xml"); foreach ($data->anime AS $anime) { $namestr = ";;"; foreach ($anime->title AS $annoying) { echo "**********<BR>"; var_dump($annoying); echo "<BR>**********<BR><BR>"; $ass = $annoying->children('xml', true); var_dump($ass); echo "<BR><BR>Title type: {$annoying["type"]}<BR>"; echo "Language: {$annoying["lang"]}<BR>"; echo "Language: {$annoying["xml:lang"]}<BR>"; echo "Language: {$ass["lang"]}<BR>"; echo "Language: {$ass->lang}<BR>"; echo "Language: {$ass->attributes["lang"]}<BR>"; echo "Language: {$ass["attributes"]["lang"]}<BR>"; echo "Language: {$ass["@attributes"]["lang"]}<BR>"; echo "<BR><HR><HR><BR>"; } } The var names kinda express my frustration at my attempts to figure this out lol. For example, this is the first block of output I get: ********** object(SimpleXMLElement)#8 (2) { ["@attributes"]=> array(1) { ["type"]=> string(5) "short" } [0]=> string(4) "CotS" } ********** object(SimpleXMLElement)#9 (1) { ["@attributes"]=> array(1) { ["lang"]=> string(2) "en" } } Title type: short Language: Language: Language: Language: Language: Language: Language: I can freaking see the data I want in the second var dump - the "en" is what I want to see. But I cannot for the life of me figure out how to actually address it outside of the var dump! Please save me from going bald Quote Link to comment Share on other sites More sharing options...
Solution requinix Posted May 16, 2013 Solution Share Posted May 16, 2013 (edited) $ass = $annoying->children('xml', true);Close. Did you know there's a similar method just for attributes? Edited May 16, 2013 by requinix Quote Link to comment Share on other sites More sharing options...
Strahan Posted May 16, 2013 Author Share Posted May 16, 2013 Ahh, thanks, your link led me down the right path After reading it I tried echo "Please freaking work: " . $annoying->attributes('xml', TRUE)->lang . "<BR>"; and now as it loops I'm seeing the actual language codes, w00t! Thanks again Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.