使用子节点解析xml
早上好 我有下一个包含子child的xml文件,我想读取它们,以便将数据填充到表中。现在,我知道如何用一种简单的方法来实现,使用childnodes,但是我真的不知道如何获得子childnodes使用子节点解析xml,xml,xml-parsing,Xml,Xml Parsing,早上好 我有下一个包含子child的xml文件,我想读取它们,以便将数据填充到表中。现在,我知道如何用一种简单的方法来实现,使用childnodes,但是我真的不知道如何获得子childnodes <Result> <Record id="000231"> <AC_NPD/> <Name>Company1</Name> <AC_CPR>00003</AC_CPR> <AC_ALM>00</AC_
<Result>
<Record id="000231">
<AC_NPD/>
<Name>Company1</Name>
<AC_CPR>00003</AC_CPR>
<AC_ALM>00</AC_ALM>
<AC_FEC>12/01/2007</AC_FEC>
<AC_LNA ncols="6">
<Row>
<Column>000084</Column>
<Column>1.230</Column>
<Column/>
<Column/>
<Column/>
<Column/>
</Row>
</AC_LNA>
<AC_FSE>12/01/2007</AC_FSE>
<AC_AV/>
<AC_UFH ncols="3"/>
</Record>
</Result>
“”;文件。写(“”);
文件。填写(“”);
}
文件。填写(“”);
因此,如果有人知道如何获得子节点,我将非常感激
致意。首先,请注意您的XML无效:
柱:
,
上面的代码创建了come表(为了简单起见,不带)并获取name、AC_CPR和每一行/列值。您可以轻松地将其展开以检索更多信息。您好,我正在使用您提交的xlst代码,它工作正常!您知道我可以用ajaxslt加载外部xml吗?您可以在文件/test/xslt_script.js中找到示例。基本上,您需要调用xmlParse(字符串)其中“string”是您的XML文档。您可以使用它来获取它。您是指这一行吗?var XML=xmlParse(el('XML').value);例如…var XML=xmlParse(myfile.XML);是的,这一行。但是您不能将文件路径放在那里,它必须是此文件的内容。请参阅了解如何使用AJAX获取文件。
<script>
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","xml/pruebas/resp2.asp",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
document.write("<table class='table table-striped table-condensed table-bordered' id='example'>");
document.write("<thead>");
document.write("<tr class='odd gradeX'>");
document.write("<th>Sale</th>");
document.write("<th>Name</th>");
document.write("<th>Date</th>");
document.write("<th>Date Sale</th>");
document.write("<th>Item</th>");
document.write("<th>Quantity</th>");
document.write("<th class='hidden-phone'>Price</th>");
document.write("<th class='hidden-phone'>Total</th>");
document.write("<th>Sale Item</th>");
document.write("<th>Button</th>");
document.write("</tr>");
document.write(" </thead>");
var x=xmlDoc.getElementsByTagName("Record");
for (i=0;i<x.length;i++)
{
document.write("<tr>");
document.write("<td>"); document.write("</td>");
document.write("<td>"); document.write(x[i].getElementsByTagName("Name")[0].childNodes[0].nodeValue); document.write("</td>");
document.write("<td>"); document.write(x[i].getElementsByTagName("AC_FEC")[0].childNodes[0].nodeValue); document.write("</td>");
document.write("<td>"); document.write(x[i].getElementsByTagName("AC_FSE")[0].childNodes[0].nodeValue); document.write("</td>");
document.write("<td>"); document.write(x[i].getElementsByTagName("AC_LNA")[0].childNodes[0].nodeValue); document.write("</td>");
document.write("<td>"); document.write("</td>");
document.write("<td>"); document.write("</td>");
document.write("<td>"); document.write("</td>");
document.write("<td>"); document.write("</td>");
document.write("<td> <a data-toggle='modal' class='btn' href='sale.asp?&number="); document.write(x[i].getElementsByTagName("AC_FEC")[0].childNodes[0].nodeValue); document.write("' data-target='#myModal'> My Sale </a> "); document.write(" </td>");
document.write("</tr>");
}
document.write("</table>");
</script>
var columns = x[i].getElementsByTagName('Column');
for ( var ci = 0; ci < columns.length; ci++ ) {
// do something with columns[ci]
}
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<table>
<xsl:for-each select="Result/Record">
<tr>
<td><xsl:value-of select="Name"/></td>
<td><xsl:value-of select="AC_CPR"/></td>
<td>
Columns:
<xsl:for-each select="AC_LNA/Row/Column[text() != '']">
<xsl:value-of select="." />,
</xsl:for-each>
</td>
</tr>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>