簡體   English   中英

HTML表單輸入字段,用於讀寫XML文件

[英]HTML Form Input Fields to read and write XML file

我目前正在嘗試使用輸入字段構建HTML表單,以讀取XML文件並寫回更改。 第一步是將頁面加載時的值檢索到輸入字段中,但它不起作用

    <body>
<h1>Config Page</h1>
<div>
    <b>Site URL:</b> <input type="text" id="siteURL" value="site..."/></span><br>
    <b>Site Collection:</b> <span id="siteCollection"></span><br>
</div>

<script>
        var xmlhttp, xmlDoc;
        xmlhttp = new XMLHttpRequest();
        xmlhttp.open("GET", "/Configuration/config.xml", false);
        xmlhttp.send();
        xmlDoc = xmlhttp.responseXML;
        document.getElement
        document.getElementById("siteURL").value.innerHTML =
        xmlDoc.getElementsByTagName("siteURL")[0].childNodes[0].nodeValue;
        document.getElementById("siteCollection").innerHTML =
        xmlDoc.getElementsByTagName("siteCollection")[0].childNodes[0].nodeValue;
        function myFunction() {
            alert(siteURL + "is the site Url")
    }
</script>
<button onclick="myFunction()">Get message value</button>

我知道XML可以順利通過,因為siteCollection范圍項目有效,但輸入字段無效。

任何幫助將非常感激。

謝謝。

也許如果您使用jQuery,可以按照以下步驟進行操作

http://codepen.io/Daethe/pen/dXWjJo

<div>
    <b>Site URL:</b> <input type="text" id="siteURL" value="site..."/></span><br>
</div>
<button onclick="myFunction()">Get message value</button>
<script>
  function myFunction() {
  var xmlHttp = jQuery.parseXML('<?xml version="1.0" encoding="utf-8"?><config><siteURL>http://localhost/</siteURL></config>');
  var xmlDoc;
  xmlDoc = xmlHttp.documentElement;
  $("#siteURL").val(xmlDoc.getElementsByTagName("siteURL")[0].childNodes[0].nodeValue);
}
</script>

感謝大德使我走上正確的道路。 我找到了將XML文件讀入HTML輸入字段形式的解決方案

對於javascript ...

var xmlpath = "../configuration/test.xml"
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", xmlpath, false);
xmlhttp.setRequestHeader('Content-Type', 'text/xml');
xmlhttp.send("");
xmlDoc = xmlhttp.responseXML;

function loadFunction() {
$("#siteURL").val(xmlDoc.getElementsByTagName("siteURL")[0].childNodes[0].nodeValue);
}

對於頁面...

    <script src="/Script/form.js"></script>
<link rel="stylesheet"     href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
                <html>
<br>Site URL (EG: http://intranet)</br>
                <input type="text" id="siteURL" name="siteURL" value="blank..." />
<button onclick="loadFunction()">Load Configuration</button>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<script type="text/javascript" src="https://code.jquery.com/jquery-2.1.3.js"></script>
<title>Please Check Data</title>
<script type="text/javascript">
    function readXMLFile() {
        var i;
        var xml = new XMLHttpRequest();
        xml.open('GET', 'projectRelatedData.xml', false);
        xml.send();
        var xmlData = xml.responseXML;
            xmlData=(new DOMParser()).parseFromString(xml.responseText,'text/xml');
        var projectData=xmlData.getElementsByTagName("project");
        //alert(projectData.length);
        for(i=0;i<projectData.length;i++)
            {
        var name=projectData[i].getElementsByTagName("name")[0].firstChild.data;
        var imageName=projectData[i].getElementsByTagName("imagePath")[0].firstChild.data;
        var pdfName=projectData[i].getElementsByTagName("pdfPath")[0].firstChild.data;
        var description=projectData[i].getElementsByTagName("description")[0].firstChild.data;
        var amount=projectData[i].getElementsByTagName("amount")[0].firstChild.data;
        //alert("number of Project : "+projectData.length);
        document.write(name+'<br>');
        document.write(imageName+'<br>');
        document.write(pdfName+'<br>');
        document.write(description+'<br>');
        document.write(amount+'<br>');
            }
    }
</script>
</head>
<body>
    <button onclick="readXMLFile()">Click</button>
    <p id="ccc"></p>
</body>
</html>



<?xml version="1.0" encoding="UTF-8"?>
<projectWebsite>
    <project>
        <name>CARGO SHIPPING</name>
        <imagePath>CARGOSHIPPING.PNG</imagePath>
        <pdfPath>cargoShipping.pdf</pdfPath>
        <description>
        Cargo shipping is all about booking cargo to move from one place to another. Owner can add new shipsand he can also track ships.User can register for cargo and he can also track ships.Admin has the right to view detailsof owner, to add a new company and also update price of ship. This software hasa very nice GUI to give a very nice presentation of a cargo management system.
        </description>
        <amount>4000</amount>
    </project>
    <project>
        <name>E-BAZZAR</name>
        <imagePath>ebazzar.PNG</imagePath>
        <pdfPath>eBazar.pdf</pdfPath>
        <description>
        This project emphasizes on taking bookings of pat ient in a web portal system.Patient can search for the doctor and book for appointment . Doctor can check and confirm appointment so patient can visit accordingly.Also admin is provided to add doctors in the portal,moreover customer list can be seen as well.
        </description>
        <amount>4000</amount>
    </project>
</projectWebsite>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM