In javascript, XML File Create, File Save

Joined
Jul 9, 2023
Messages
25
Reaction score
0
Hello.

In Javascript, XLM Create, XML File Save.
xmlex1.png


Code:
<root>
    <SCHOOL id = THOMAS JEFFERSON>
        <NAME id = "James"> 160 </NAME>
        <NAME id = "Mateo"> 157 </NAME>
        <NAME id = "Mason"> 170 </NAME>
        <NAME id = "Henry"> 165 </NAME>
        <NAME id = "Jack"> 180 </NAME>
        <NAME id = "Christian"> 172 </NAME>
    </SCHOOL>

    <SCHOOL id = PHILLIPS EXETER ACADEMY>
        <NAME id = "Micah"> 145 </NAME>
        <NAME id = "Jordan"> 156 </NAME>
    </SCHOOL>

    <SCHOOL id = Deerfield Academy>
        <NAME id = "Samuel"> 189 </NAME>
        <NAME id = "Jayden"> 136 </NAME>
        <NAME id = "Jacob"> 173 </NAME>
    </SCHOOL>
  
    <SCHOOL id = Groton School>
        <NAME id = "Greyson"> 159 </NAME>
        <NAME id = "Sntiago"> 163 </NAME>
        <NAME id = "Colton"> 189 </NAME>
        <NAME id = "Roman"> 201 </NAME>
        <NAME id = "Parker"> 156 </NAME>
    </SCHOOL>
  
    <SCHOOL id = Milton Academy>
        <NAME id = "Cameron"> 159 </NAME>
    </SCHOOL>
                    :
                    :
                    :
                    :
                Infinity 
</root>

Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
 <head>
  <title></title>
  <script language="javascript">
   function XMLCreate()
   {
    var xmlDoc=new ActiveXObject("microsoft.XMLDOM");
 
    //<?xml version='1.0' encoding='euc-kr'?>
    var PInode=xmlDoc.createProcessingInstruction("xml", "version='1.0' encoding='euc-kr'");
 
    //2. Root Element <compactdiscs></compactdiscs>
    var rootNode=xmlDoc.createElement("compactdiscs");
 
    //3. Element
    var compactdiscNode=xmlDoc.createElement("compactdisc");
    var titleNode=xmlDoc.createElement("title");
    var priceNode=xmlDoc.createElement("price");
 
    //4. Text Node
    var titleTextNode=xmlDoc.createTextNode("OST");
 
    var priceTextNode=xmlDoc.createTextNode("8000");
 
    //
    xmlDoc.appendChild(PInode);
    xmlDoc.appendChild(rootNode);
 
    rootNode.appendChild(compactdiscNode);
 
    compactdiscNode.appendChild(titleNode);
    compactdiscNode.appendChild(priceNode);
 
    titleNode.appendChild(titleTextNode);
    priceNode.appendChild(priceTextNode);
 
    //Attribute
    titleNode.setAttribute("numberoftracks", "3");
 
    //Output
    document.all.txtResult.value=xmlDoc.xml;
 
   }
  </script>
 </head>
 <body>
  <h3>XML Make </h3>
  <button onclick="XMLCreate()">XML Make</button>
  <br/>
  <textarea cols="80" rows="20" name="txtResult"></textarea>
 </body>
</html>
 
Last edited:
Joined
Jul 4, 2023
Messages
355
Reaction score
41
In HTML, you cannot directly read or write files on the user's computer. HTML is a language for structuring the content and presentation of a web page, not for file system operations.

However, you can use JavaScript, which is supported by most web browsers, to perform client-side operations on XML files. You can use the XMLHttpRequest object or the more modern Fetch API interface to read data from an XML file. Then, after making the necessary changes, you can save the modified XML file to the server using an appropriate server-side language like PHP, Python, Node.js, etc.
 
Joined
Jul 4, 2023
Messages
355
Reaction score
41
Here's a simple example of how to read an XML file and make changes using JavaScript.

index.html (client - front-end):
HTML:
<!DOCTYPE html>
<html lang="en">
    <head>
        <title>Example of Reading and Writing XML File</title>
    </head>
    <body>
        <button onclick="readXML()">Read XML</button>
        <button onclick="saveXML()">Save XML</button>

        <script>
            function readXML() {
                var xhr = new XMLHttpRequest();
                xhr.onreadystatechange = function() {
                    if (xhr.readyState === 4 && xhr.status === 200) {
                        var xmlData = xhr.responseXML;
                        // Here, you can perform manipulations on the data from the XML file
                        console.log(xmlData);
                    }
                };
                xhr.open("GET", "path/to/your/file.xml", true);
                xhr.send();
            }

            function saveXML() {
                // Let's assume that the modified XML file is stored in the variable `modifiedXML`
                var modifiedXML = '<root><element>New content</element></root>';
                var xhr = new XMLHttpRequest();
                xhr.onreadystatechange = function() {
                    if (xhr.readyState === 4 && xhr.status === 200) {
                        console.log('Modified XML file has been saved on the server.');
                    }      
                };      
                xhr.open("POST", "path/to/your/server/save_xml.php", true);
                xhr.setRequestHeader("Content-Type", "application/xml");
                xhr.send(modifiedXML);
            }
        </script>
    </body>
</html>

save_xml.php (server - back-end):
PHP:
<?php
    if ($_SERVER["REQUEST_METHOD"] === "POST") {
        // Read data sent via POST
        $xmlData = file_get_contents("php://input");

        // Here you can perform any operations on the XML data, e.g., save it to a file on the server
        $filePath = "path/to/your/file.xml";
        file_put_contents($filePath, $xmlData);

        // Send a response to the client indicating the successful save
        http_response_code(200);
        echo "XML file has been saved on the server.";
    } else {
        // Handling other requests (optional)
        http_response_code(404);
        echo "Page not found.";
    }
?>

the PHP file save_xml.php receives data sent via a POST request, which is stored in the variable $xmlData. Then, this data is saved to a file at a specified path on the server using the file_put_contents() function. After the file is successfully saved, the server returns a response indicating success with an HTTP 200 response code and the message "XML file has been saved on the server."
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,531
Members
45,004
Latest member
KetoBoostGummies

Latest Threads

Top