Jump to content

Need help parsing XML


Strahan

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
https://forums.phpfreaks.com/topic/278049-need-help-parsing-xml/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.