Hướng dẫn javascript export xml file - javascript xuất tệp xml

Tôi có chức năng ReadXML sẽ đọc giá trị từ tệp XML đã cho và nó sẽ thay thế giá trị nút cụ thể, sau khi thay thế giá trị cụ thể, tương tự phải được phản ánh tệp RAW (ý tôi là tệp sách.xml), sau khi thực hiện sửa đổi cách Để lưu nội dung trong tệp XML.

Ví dụ: Mã để thay thế nút.

function readXML() {

        xmlDoc = loadXMLDoc("books.xml");

        x = xmlDoc.documentElement;

        //create a book element, title element and a text node
        newNode = xmlDoc.createElement("book");
        newTitle = xmlDoc.createElement("title");
        newText = xmlDoc.createTextNode("A Notebook");

        //add the text node to the title node,
        newTitle.appendChild(newText);
        //add the title node to the book node
        newNode.appendChild(newTitle);

        y = xmlDoc.getElementsByTagName("book")[1]
        //replace the first book node with the new node
        x.replaceChild(newNode, y);

        z = xmlDoc.getElementsByTagName("title");
        for (i = 0; i < z.length; i++) {
            alert(z[i].childNodes[0].nodeValue);

        }

       //Here i have to save the xmlDoc in my local system.

    }

    function loadXMLDoc(dname) {
        if (window.XMLHttpRequest) {
            xhttp = new XMLHttpRequest();
        }
        else {
            xhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xhttp.open("GET", dname, false);
        xhttp.send();
        return xhttp.responseXML;
    }

Tôi mới đến XML.

Các cách tiếp cận để làm việc với các tệp XML trong TestComplete, để làm việc với các tệp XML trong TestComplete, bạn có thể sử dụng các đối tượng sau:, Sử dụng đối tượng Storages, để bắt đầu làm việc với các tệp XML, tạo đối tượng COM:

# If you have MSXML 4:
   Doc = Sys.OleObject["Msxml2.DOMDocument.4.0"]
# If you have MSXML 6:
   Doc = Sys.OleObject["Msxml2.DOMDocument.6.0"]
Doc.setProperty("ProhibitDTD", False)
def TestXMLDOM():
   # Create a COM object
# If you have MSXML 4:
   Doc = Sys.OleObject["Msxml2.DOMDocument.4.0"]
# If you have MSXML 6:
   Doc = Sys.OleObject["Msxml2.DOMDocument.6.0"]

Doc.async = False

# Load data from a file
# We use the file created earlier
Doc.load("C:\\Data.xml")

# Report an error,
if,
for instance, the markup or file structure is invalid
if Doc.parseError.errorCode != 0:
   s = "Reason:\t" + Doc.parseError.reason + "\n" + "Line:\t" + aqConvert.VarToStr(Doc.parseError.line) + "\n" + "Pos:\t" + aqConvert.VarToStr(Doc.parseError.linePos) + "\n" + "Source:\t" + Doc.parseError.srcText
# Post an error to the log and exit
Log.Error("Cannot parse the document.", s)
return

# Obtain the node
Node = Doc.documentElement

# Process the node
ProcessNode(Node)

def ProcessNode(ANode):

   # Create a log folder
for each node and activate the folder
FID = Log.CreateFolder(ANode.nodeName)
Log.PushLogFolder(FID)

# If the node value is not null, output it
if aqObject.GetVarType(ANode.nodeValue) != 0 and aqObject.GetVarType(ANode.nodeValue) != 1:
   Log.Message("Value: " + aqConvert.VarToStr(ANode.nodeValue))

# Process the node 's attributes

# Exclude helper nodes from processing
if ANode.nodeName[0] != "#":
   # Obtain the attribute collection and
# output the attributes to the log
Attrs = ANode.attributes
for i in range(Attrs.length):
   Attr = Attrs.item[i]
Log.Message("Attr " + Attr.nodeName + ": " + Attr.nodeValue)

# Obtain the collection of child nodes
ChildNodes = ANode.childNodes
# Processes each node of the collection
for i in range(ChildNodes.length):
   ProcessNode(ChildNodes.item[i])

# Close the log folder
Log.PopLogFolder()
def TestWithXPath():

   # Create a COM object
# If you have MSXML 4:
   Doc = Sys.OleObject["Msxml2.DOMDocument.4.0"]
# If you have MSXML 6:
   Doc = Sys.OleObject["Msxml2.DOMDocument.6.0"]

Doc.async = False

# Load data from a file
# We use the file created earlier
Doc.load("C:\\Data.xml")

# Report an error,
if,
for instance, the markup or file structure is invalid
if Doc.parseError.errorCode != 0:
   s = "Reason:\t" + Doc.parseError.reason + "\n" + "Line:\t" + aqConvert.VarToStr(Doc.parseError.line) + "\n" + "Pos:\t" + aqConvert.VarToStr(Doc.parseError.linePos) + "\n" + "Source:\t" + Doc.parseError.srcText
# Post an error to the log and exit
Log.Error("Cannot parse the document.", s)
return

# Use an XPath expression to obtain a list of "control"
nodes
Nodes = Doc.selectNodes("//control")

# Process the node
for i in range(Nodes.length):
   # Get the node from the collection of the found nodes
Node = Nodes.item[i]
# Get child nodes
ChildNodes = Node.childNodes
# Output two child nodes to the log
Log.Message(ChildNodes.item[1].text + ": " + ChildNodes.item[2].text)

Gợi ý: 2

Đầu tiên, chúng ta hãy đọc tệp data.xml, trong thư mục gốc của thư mục của bạn. Tất cả những gì chúng ta cần làm là nhập gói FS và sử dụng hàm readfile., Và sau đó CD vào thư mục mới của bạn:, Tiếp theo, hãy tạo một tệp XML trong gốc của dự án của bạn mà chúng tôi sẽ sử dụng cho mục đích thử nghiệm:, Hãy nhanh chóng tạo một thư mục cho mã của chúng tôi:

    npm install xml2json xml - formatter--save

Gợi ý: 3

Tôi có chức năng ReadXML sẽ đọc giá trị từ tệp XML đã cho và nó sẽ thay thế giá trị nút cụ thể, sau khi thay thế giá trị cụ thể, tương tự phải được phản ánh tệp RAW (ý tôi là tệp sách.xml), sau khi thực hiện sửa đổi cách Để lưu nội dung trong tệp XML.

var blob = new Blob([xmpString], {
   type: "text/xml"
});

saveAs(blob, "test.xmp");

Gợi ý: 4

let jsrefstring;
document.getElementById('test_1')
   .addEventListener('change', function() {
      var fr = new FileReader();
      fr.onload = function() {
         jsrefstring = (fr.result).toString();
      }
      fr.readAsText(this.files[0]);
   });


const fs = require("fs");
fs.readFileSync("test.txt");


const fs = require("fs");

fs.open('test.txt', 'test', function(err, file) {
   if (err) throw err;
});


const fs = require("fs");
fs.readFileSync("test.txt", "utf8");
fs.appendFile("test.txt", "test", function(err) {
   if (err) {
      throw err;
   }
})

Gợi ý: 5

Nội dung này, cùng với bất kỳ mã nguồn và tệp liên quan nào, được cấp phép theo giấy phép mở của Dự án Mã (CPOL), nếu một câu hỏi kém thì yêu cầu làm rõ, bỏ qua hoặc chỉnh sửa câu hỏi và khắc phục sự cố. Những lời lăng mạ không được chào đón., Đừng bảo ai đó đọc hướng dẫn. Rất có thể họ có và không nhận được nó. Cung cấp câu trả lời hoặc chuyển sang câu hỏi tiếp theo. , Cứu giúp? 'Codeproject' là gì? Câu hỏi thường gặp nói chung đặt một câu hỏi về lỗi và đề xuất bài viết giúp diễn đàn về chúng tôi

if (window.XMLHttpRequest) {
   xhttp = new XMLHttpRequest();
} else 
{
   xhttp = new ActiveXObject("Microsoft.XMLHTTP");
}

xhttp.open("GET", "books.xml", false);
xhttp.setRequestHeader("Accept", "text/xml");
xhttp.send();
xmlDoc = xhttp.responseXML;
x = xmlDoc.getElementsByTagName("title")[0].childNodes[0];

x.nodeValue = "Easy Cooking";
xmlDoc.Save("books.xml");

Bạn có thể sử dụng XML với JavaScript không?

Với một vài dòng mã JavaScript, bạn có thể đọc tệp XML và cập nhật nội dung dữ liệu của bất kỳ trang HTML nào...

Làm cách nào để lưu tệp XML trong Chrome?

Chỉ cần nhấp vào nút Tệp (3 dòng) và nhấp vào Lưu trang dưới dạng.Ví dụ: tôi đã truy cập xml-sitemaps.com/sitemap.xml và nhấp vào Lưu trang dưới dạng.Nó được lưu dưới dạng XML vào máy cục bộ của tôi và được tải như vậy.click the File button (the 3 lines), and click Save Page As. For example, I went to xml-sitemaps.com/sitemap.xml and clicked Save Page As. It saved as XML to my local machine and loaded as such.click the File button (the 3 lines), and click Save Page As. For example, I went to xml-sitemaps.com/sitemap.xml and clicked Save Page As. It saved as XML to my local machine and loaded as such.