如何在解析xml文件时替换此处收到的值

时间:2014-05-21 14:05:40

标签: javascript jquery

我有以下xml文件

<categories>

  <category id="2" name="Pepsi" >
     <products>
      <product id="858" name="7UP" price="24.4900" />
      <product id="860" name="Aquafina" price="24.4900" />
      </products>
  </category>

  <category id="4" name="Coke" >
     <products>
      <product id="811" name="ThumpsUp" price="24.4900" />
      <product id="813" name="Maaza" price="24.4900" />
    </products>
  </category>

 </categories>

我正在使用Jquery

解析它
$(xmldocu).find("category").each(function () {
    var names = $(this).attr('name');//.text();
     alert(names);
    });

请在这里查看JSFiddle http://jsfiddle.net/GTf6c/

如何使用解析xml时收到的类别元素创建activateUi.newPanel.html

activateUi.newPanel.html(" \
                    <h3><a href='#'>First</a></h3> \
                    <div></div> \
                    <h3><a href='#'>Second</a></h3> \
                    <div></div> \
                    <h3><a href='#'>Third</a></h3><div></div>")

1 个答案:

答案 0 :(得分:1)

你可以这样做:

var names = []; //make an empty array to store the category names
$(xmldocu).find("category").each(function () {
    names.push($(this).attr('name')); //add each category name to the array
});

var activateUiHTML = $("<div></div>"); //make an empty div to store the new HTML
for (var i = 0; i < names.length; i++) {
    // loop through each name, and create the desired markup for each category name
    activateUiHTML.append("<h3><a href='#'>" + names[i] + "</a></h3><div></div>");
}

activateUi.newPanel.html(activateUiHTML); //set the new markup as the html