0

i want to store xml Dom object in array and retrieve them back from array using array index

for example

arrayoftags[index] = $this->dom->createElement("plist");
index++;

// and retrive back 

$dict = $this->dom->createElement("dict");
arrayoftags[index]->appendChild($dict);

/* some thing like that
 <plist>
   <dict>
   </dict>
 </plist>
*/

what i am doing wrong please guide me in right direction and thanks in advance

Anni
  • 142
  • 2
  • 16

2 Answers2

0

Please refer this code, I think this will help you.

<!-- suppose this is book.xml file -->
<?xml version="1.0"?>
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications
with XML.</description>
 </book>
 <book id="bk102">
<author>Ralls, Kim</author>
<title>Midnight Rain</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-12-16</publish_date>
<description>A former architect battles corporate zombies,
an evil sorceress, and her own childhood to become queen
of the world.</description>
 </book>
 <catalog>

//PHP file
$dom = new DOMDocument();
$dom->loadXml('book.xml');
$xpath = new DOMXpath($dom);
 $result = [];
 foreach ($xpath->evaluate('//book') as $book) {
 $result[] = [
'id' => $xpath->evaluate('string(@id)', $book),
'Author' => $xpath->evaluate('string(author)', $book),
'Title' => $xpath->evaluate('string(title)', $book),
'Genre' => $xpath->evaluate('string(genre)', $book),
'Price' => $xpath->evaluate('number(price)', $book),
'Publish Date' => $xpath->evaluate('string(publish_date)', $book),
'Description' => $xpath->evaluate('string(description)', $book)
];
}
var_dump($result);
Sanjay
  • 1,958
  • 2
  • 17
  • 25
  • sorry Sanjay but this will never meet my scenario – Anni Mar 11 '14 at 07:12
  • Ok Anni, please check this link http://www.kirupa.com/forum/showthread.php?304580-store-XML-data-in-Array – Sanjay Mar 11 '14 at 07:18
  • This looks like you copied my example from http://stackoverflow.com/questions/22301087/how-to-import-xml-using-php/22302817#22302817. – ThW Mar 11 '14 at 07:37
0

I am not sure why you want to use an array. So the answer is a little more generic. But yes you can store XML nodes in variables including arrays.

$dom = new DOMDocument();

$created = [];
$created['plist'] = $dom->appendChild($dom->createElement('plist'));

$created['dict'] = $dom->createElement('dict');
$created['plist']->appendChild($created['dict']);


echo $dom->saveXml();

Output:

<?xml version="1.0"?>
<plist><dict/></plist>

appendChild() returns the node it appended. So it is possible to use it directly on a createElement() (or other create* call) and assign the result to a variable. So if the parent node is just stored in a variable the example will be cleaner.

$dom = new DOMDocument();

$plist = $dom->appendChild($dom->createElement('plist'));
$plist->appendChild($dom->createElement('dict'));

echo $dom->saveXml();

Now the DOM already is a data structure, you can use Xpath to fetch some nodes from it, why store the nodes in a second structure (the array)?

$dom = new DOMDocument();

$plist = $dom->appendChild($dom->createElement('plist'));
$plist->appendChild($dom->createElement('dict'));

$xpath = new DOMXpath($dom);
foreach ($xpath->evaluate('//*') as $node) {
  var_dump($node->nodeName);
}

Output:

string(5) "plist"
string(4) "dict"
ThW
  • 19,120
  • 3
  • 22
  • 44