在js文件中使用AJAX获取动态创建的XML值

时间:2014-04-29 14:07:50

标签: javascript php ajax xml

我用php创建了一个动态xml文件,如下所示

('Content-Type: text/xml');?>
<?php $newitem = $_GET["book"];
$action = $_GET["action"];
$qty=0;
$isbnVal="XYZ";
if ($_SESSION["Cart"] != ""){
 $MDA = $_SESSION["Cart"];
 if ($action == "Add"){
  if ($MDA[$newitem] != ""){  
  $tempValue = $MDA[$newitem];
  $value=$tempValue["qty"]+1;
  $MDA[$newitem] =array("qty" =>  $value,"isbn" => $isbnVal);
  }else{$MDA[$newitem] =array("qty" => 1,"isbn" => $isbnVal);}
  }else{$MDA= "";}
}
else{$MDA[$newitem] =array("qty" => 1,"isbn" => $isbnVal);}
$_SESSION["Cart"] = $MDA; 
ECHO (toXml($MDA));                                         
function toXml($MDA){
 $doc = new DomDocument('1.0');
 $cart = $doc->createElement('cart');
 $cart = $doc->appendChild($cart);
 foreach ($MDA as $a => $b){
  //echo "isbn".$b["isbn"];//echo "qty".$b["qty"];  $book = $doc->createElement('book');
 $book = $cart->appendChild($book);

 $title = $doc->createElement('title'); 
 $title = $book->appendChild($title);   
 $value = $doc->createTextNode($a);
 $value = $title->appendChild($value);


 $quantity = $doc->createElement('quantity');
 $quantity = $book->appendChild($quantity);

 $value2 = $doc->createTextNode($b["qty"]);
 $value2 = $quantity->appendChild($value2);

 $isbn = $doc->createElement('isbn');
 $isbn = $book->appendChild($isbn);
 $value3 = $doc->createTextNode($b["isbn"]);
 $value3 = $isbn->appendChild($value3);
 }

        $strXml = $doc->saveXML(); 
        return $strXml;
    }

然后我在javaScript文件中调用这些值来显示。我可以从firstChild和lastChild获取第一个和最后一个值。但我不能得到中间的任何东西。我尝试了如下。

spantag.innerHTML += " " +header[0].firstChild.textContent;
spantag.innerHTML += " " +header[0].getElementsByTagName('qty');
spantag.innerHTML += " " + header[0].lastChild.textContent + "  <a href='#' onclick='AddRemoveItem(\"Remove\");'>Remove Item</a> ";

只有第二个代码不起作用。 Plz告诉我我做错了什么。提前谢谢。

0 个答案:

没有答案