asaschool Posted December 7, 2008 Share Posted December 7, 2008 Hey everyone, I am having trouble with a simple task and its driving me nuts. I need to load the contents of an XML file into an array so I can loop through it and insert them into my database. I need to have the name attribute of <word>(ie: "work title") and the contents of <word> (ie: "this is the data") in separate variables/arrays. Can anybody help me with this? I cant seem to set up my script right to pull them into variables. I am starting over and thought I would come here first, before spending another day of scripting. XML Example: <glossary> <word name="work title"> <![CDATA[This is the data.]]> </word> <word name="work title"> <![CDATA[This is the data.]]> </word> <word name="work title"> <![CDATA[This is the data.]]> </word> </glossary> Thanks in advance. Link to comment https://forums.phpfreaks.com/topic/135937-loading-contents-of-xml-file-into-variablesarrays/ Share on other sites More sharing options...
premiso Posted December 7, 2008 Share Posted December 7, 2008 dom Take a look at the PHP DOM, I think that is what you would want. An example from the DOMDocument comments <?php function xml2array($xml) { $domDocument = new DOMDocument; $domDocument->loadXML($xml); $domXPath = new DOMXPath($domDocument); $array = array(); foreach ($domXPath->query('//key') as $keyDOM) { $id = $keyDOM->getAttribute('id'); $value = $keyDOM->hasAttribute('value') ? $keyDOM->getAttribute('value') : trim($keyDOM->textContent); if (array_key_exists($id, $array)) { if (is_array($array[$id])) { $array[$id][] = $value; } else { $array[$id] = array($array[$id]); $array[$id][] = $value; } } else { $array[$id] = $value; } } return $array; } ?> Link to comment https://forums.phpfreaks.com/topic/135937-loading-contents-of-xml-file-into-variablesarrays/#findComment-708601 Share on other sites More sharing options...
asaschool Posted December 8, 2008 Author Share Posted December 8, 2008 Hello Everyone, I got my script working on my earlier issue and now I am having another roadblock with my new task. I need to load the data into variables that I can work with. I have one area that is giving me a problem and I wanted to see if anybody can help. Here is my new xml data breakdown that I am working with. <topics> <item id="1" answer_id="B"> <question><![CDATA[This is where the Main data is located]]></question> <replies> <reply id="A"><![CDATA[Answer Option A]]> </reply> <reply id="B"><![CDATA[Answer Option B]]> <description><![CDATA[Explination of question]]></description> </reply> <reply id="C"><![CDATA[Answer Option C]]></reply> <reply id="D"><![CDATA[Answer Option D]]></reply> </replies> </item> </topics> I can get all of the data loaded into variable except for all four of the <reply>'s. I can find the first one, but not the following three others. Can anybody let me know what I am doing wrong. How should I be calling the data for these <reply> items? Here is the php code I am working with. PHP Code: <?php error_reporting(E_ALL); include 'xmlFile.php'; $xml = new SimpleXMLElement($xmlstr); $text = $xmlstr; $xml = simplexml_load_string($text, 'SimpleXMLElement', LIBXML_NOCDATA); foreach($xml->item as $item) { foreach ($item->question as $question) { foreach ($item->replies as $replyGroup) { foreach ($replyGroup->reply as $replyMain) { // This gives me the first reply but not the rest foreach ($replyMain->description as $desc) { } } } } echo "Situation : ".$question."<br>Correct Reponse: ".$replyMain['id']."<br>Why: ".$desc."<br>Reply One: ".$replyMain."<br>Reply Two: <br>Reply Three: <br>Reply Four: <br><br>"; } ?> Thanks in advance Link to comment https://forums.phpfreaks.com/topic/135937-loading-contents-of-xml-file-into-variablesarrays/#findComment-708959 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.