map an XML string into HTML page with jquery
I have a XML string stored as a javascript string.
<script src="jquery.mobile/jquery.js"></script>
<script>
testXML="<?xml version='1.0' en开发者_StackOverflow社区coding='UTF-8' ?>\n\
<DataGroup>\n\
<RequestHdr>\n\
<XORN>98765</XORN>\n\
<STDE>20110101</STDE>\n\
<ARTE>20110102</ARTE>\n\
</RequestHdr>\n\
</DataGroup>";
$(document).ready(function()
{
});
</script>
and I want to map this directly into an HTML fragment that looks like this.
<input type="text" name="XORN" id="preXord" value="" />
<input type="text" name="STDE" id="preTrav" value="" />
<input type="text" name="ARTE" id="preArr" value="" />
I am assuming that you are just wanting the values in xorn, stde, and arte (xml) to be used in the input values
you can do
var $xml = $.parseXML( testXML );
$('#preXord').val($xml.find('XORN').text());
...
var xml="<?xml version='1.0' encoding='UTF-8' ?>\n\
<DataGroup><RequestHdr><XORN>98765</XORN><STDE>20110101</STDE><ARTE>20110102</ARTE></RequestHdr></DataGroup>";
xmlDoc = $.parseXML( xml ),
$xml = $( xmlDoc );
$("#preXord").val($xml.find("XORN").text());
$("#preTrav").val($xml.find("STDE").text());
$("#preArr").val($xml.find("ARTE").text());
here is the working fiddle http://jsfiddle.net/wBJyQ/
Take a look at jQuery parseXML that will help you in this case and also make you understand how it works.
精彩评论