0

I put 3 files in same folder: jquery-1.10.2.js, info.xml and info.htm.

This is my info.xml:

<Check>
    <Metainformation>
        <cash>ABC XYZ</cash>
        <Doctor>Dr. Peter Smith</Doctor>
        <DoctorID>12345678</DoctorID>
        <Quartal>Q22004</Quartal>
        <Checkdate>20040404123000</Checkdate>
    </Metainformation>
    <Patientlist>
        <Normal_Patient>
            <Unchanged count="123" />
            <New count="3" />
            <Closed count="2" />
            <InTest count="4" />
        </Normal_Patient>
        <Special_Patient>
            <Special_Quantity count="8" />
        </Special_Patient>
        <Notfound_Patient>
            <ABC_available count="9" />
            <DEF_available count="7" />
        </Notfound_Patient> 
        <Total old="125" new="126"/>
    </Patientlist>
</Check>

This is my info.htm

<html>
<head>
<meta http-equiv=Content-Type content="text/html; charset=windows-1252">
<meta name="Daniel" content="Geneva">
<script language="JavaScript" type="text/javascript" src="jquery-1.10.2.js"></script>
<style type="text/css">
.frame {
    font-size: 10pt;
}
.div1 {
    margin-right:80px;
    margin-left:80px;
}
.table1 {
    width: 640px;
    margin: 0 auto;
    border-collapse: collapse;
    font-weight: bold;
}
.table1 td {
    vertical-align: top;
    padding-left: 6px;
    padding-bottom: 5px;
    border: 1px solid black;
    width: 320px;  
}
.table1 tr:nth-child(2) td {
    border: none;
}
</style>
</head>
<body>
<div class ='frame'>
    <div class='div1'>
        <table class='table1' style ='margin-top:20px'>
            <tr>
                <td>Contract</td>
                <td id="cash"> </td>
            </tr>
            <tr>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
            </tr>
            <tr>
                <td>Doctor</td>
                <td id="Doctor"> </td>
            </tr>
            <tr>
                <td>Quartal</td>
                <td id="Quartal"> </td>
            </tr>
            <tr>
                <td>Test carried out by</td>
                <td id="Doctor1"></td>
            </tr>
            <tr>
                <td>Checkdate</td>
                <td id="Date"></td>
            </tr>
        </table>
    </div>
</div>

<script>
var xml = "info.xml";
xmlDoc = $.parseXML(xml);
$xml = $(xmlDoc);
$doctor = $xml.find("Doctor");
$id = $xml.find("DoctorID");
var doc = $doctor.text() + "(ID: " + $id.text() + ")";  
$("#Doctor").append(doc);
$("#Doctor1").append(doc);
</script>

</body>
</html>

I need to load Doctor and DoctorID from info.xml and put together into field Doctor and Doctor1. But it does not work. Could you guys tell me where I am wrong here?

trangan
  • 341
  • 8
  • 22

1 Answers1

1

File open?

$.get('info.xml', function(xml){
  xmlDoc = $.parseXML(xml);
  $xml = $(xmlDoc);
  $doctor = $xml.find("Doctor");
  $id = $xml.find("DoctorID");
  var doc = $doctor.text() + "(ID: " + $id.text() + ")";  
  $("#Doctor").append(doc);
  $("#Doctor1").append(doc);  
});

If xml is in a variable, then it needs to be processed

var xml="<Check><Metainformation><cash>ABC XYZ</cash><Doctor>Dr. Peter Smith</Doctor><DoctorID>12345678</DoctorID><Quartal>Q22004</Quartal><Checkdate>20040404123000</Checkdate></Metainformation><Patientlist><Normal_Patient><Unchanged count='123' /><New count='3' /><Closed count='2' /><InTest count='4' /></Normal_Patient><Special_Patient><Special_Quantity count='8' /></Special_Patient><Notfound_Patient><ABC_available count='9' /><DEF_available count='7' /></Notfound_Patient> <Total old='125' new='126'/></Patientlist></Check>";
var xmlDoc = $.parseXML(xml)
$xml = $( xmlDoc ),
$doctor = $xml.find("Doctor");
$id = $xml.find("DoctorID");
var doc = $doctor.text() + "(ID: " + $id.text() + ")";  
$("#Doctor").append(doc);
$("#Doctor1").append(doc);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class ='frame'>
    <div class='div1'>
        <table class='table1' style ='margin-top:20px'>
            <tr>
                <td>Contract</td>
                <td id="cash"> </td>
            </tr>
            <tr>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
            </tr>
            <tr>
                <td>Doctor</td>
                <td id="Doctor"> </td>
            </tr>
            <tr>
                <td>Quartal</td>
                <td id="Quartal"> </td>
            </tr>
            <tr>
                <td>Test carried out by</td>
                <td id="Doctor1"></td>
            </tr>
            <tr>
                <td>Checkdate</td>
                <td id="Date"></td>
            </tr>
        </table>
    </div>
</div>
Redr01d
  • 392
  • 2
  • 12
  • thanks a lot redroid! :) I accept your answer. But I still have a small question. I dont want to paste all the info.xml like that `var xml="...."`. But if i use the function `$.get('info.xml', function(xml){}` then I get error message:`Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.` How could I solve this? – trangan Apr 18 '17 at 11:29
  • xml in local mashine? – Redr01d Apr 18 '17 at 11:54
  • yes. info.xml is in my local machine. I prefer to use the function `$.get('info.xml', function(xml){}` because it looks like programming with semicolon. So it is easier for me to observe :D – trangan Apr 18 '17 at 11:56
  • install a webserver in your local PC http://stackoverflow.com/questions/10752055/cross-origin-requests-are-only-supported-for-http-error-when-loading-a-local – Redr01d Apr 18 '17 at 11:58
  • Thanks, but the problem here is, the content will be shown not only in my local machine, but also for other computers. So in case, other machines do not have webserver, then it does not help. :) – trangan Apr 18 '17 at 12:01
  • Then you need hosting – Redr01d Apr 18 '17 at 12:05
  • could you tell me the simple way for hosting, or another simple way to deal with it. Because i am not good at networking. :) – trangan Apr 18 '17 at 12:06