Jump to content

Need help parsing XML


Strahan
Go to solution Solved by requinix,

Recommended Posts

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 :)

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.