简体   繁体   中英

Display XML/JSON content in HTML page

I am newbie , I have application in which check-box tree is present. I want to pre-populate the check-box's if user before checked some of those check box.

For that am getting XML format from my back-end perl script as shown below.like , in below XML only 0, 43,44,45,46 and 50 are coming so only those respective checkbox need to checked on page load.I want to display those checked check-box on page load .How can I do this .......?

I tried with so many examples . but am not getting solution for my problem .Below is my XML format data which is am getting from perl script .

<perldata> 
<hashref memory_address="0x86f4880"> 
<item key="0">1</item> 
</hashref> 
</perldata> 
<perldata> 
<hashref memory_address="0x86f4880"> 
<item key="43">1</item> 
</hashref>  
</perldata> 
<perldata> 
<hashref memory_address="0x86f4880"> 
<item key="44">1</item> 
</hashref> 
</perldata> 
<perldata> 
<hashref memory_address="0x86f4880"> 
<item key="45">1</item> 
</hashref> 
</perldata> 
<perldata> 
<hashref memory_address="0x86f4880"> 
<item key="46">1</item> 
</hashref>  
</perldata> 
<perldata> 
<hashref memory_address="0x86f4880"> 
<item key="50">1</item> 
</hashref> 
</perldata>

There is much information missing, but I'll try to help you out. I'm gonna make assumption that you are getting the entire XML and that you aren't using jQuery or other libraries. First your XML is missing root node. Then you can parse your XML in JavaScript and check your boxes so:

// if you are getting the XML from AJAX call
//var xmlDoc = xmlhttp.responseXML;

// if you are have the XML as String
var xmlStr = '<root><perldata> \
<hashref memory_address="0x86f4880"> \
<item key="0">1</item> \
</hashref> \
</perldata> \
<perldata> \
<hashref memory_address="0x86f4880"> \
<item key="43">1</item> \
</hashref>  \
</perldata> \
<perldata> \
<hashref memory_address="0x86f4880"> \
<item key="44">1</item> \
</hashref> \
</perldata> \
<perldata> \
<hashref memory_address="0x86f4880"> \
<item key="45">1</item> \
</hashref> \
</perldata> \
<perldata> \
<hashref memory_address="0x86f4880"> \
<item key="46">1</item> \
</hashref>  \
</perldata> \
<perldata> \
<hashref memory_address="0x86f4880"> \
<item key="50">1</item> \
</hashref> \
</perldata> \
</root>';
var xmlDoc;
if (window.DOMParser)
{
    var parser=new DOMParser();
    xmlDoc=parser.parseFromString(xmlStr,"text/xml");
}else // Internet Explorer
{
    xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
    xmlDoc.async=false;
    xmlDoc.loadXML(xmlStr); 
}

// get all the item tags from the XML
var itemTags = xmlDoc.getElementsByTagName("item");
// get all the items to be checked
var inputsToCheck = {}; 
for(var i = 0, len = itemTags.length; i < len; i++) {
    var itemTag = itemTags[i];
    inputsToCheck[itemTag.getAttribute("key")] = true;
}

// loop through the inputs and check the items to be checked
var inputs = document.getElementsByTagName("input");
for(var i = 0, len = inputs.length; i < len; i++) {
    var input = inputs[i];
    if(input.getAttribute("type") == "checkbox" && inputsToCheck[input.getAttribute("name")])
        input.setAttribute("checked", "true");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM