- Парсер xml для javascript
- Parsing and serializing XML
- Creating an XML document
- Parsing strings into DOM trees
- Parsing URL-addressable resources into DOM trees
- Using XMLHttpRequest
- Serializing an XML document
- Serializing DOM trees to strings
- Serializing HTML documents
- See also
- Found a content problem with this page?
- MDN
- Support
- Our communities
- Developers
Парсер xml для javascript
XML Parser, Stringifier and DOM
Parse XML, HTML and more with a very tolerant XML parser and convert it into a DOM.
These three components are separated from each other as own modules.
Component | Size |
---|---|
Parser | 4.7 KB |
Stringifier | 1.3 KB |
DOM | 3.1 KB |
const xml = require("xml-parse");
Just call the parse method of the xml-parse instance.
const xml = require("xml-parse");// Valid XML stringvar parsedXML = xml.parse(' ' +'Root Element');console.log(parsedXML);// Invalid XML stringvar parsedInavlidXML = xml.parse(' ' +' ' +' ' +' ');console.log(parsedInavlidXML);
The result of parse is an object that maybe looks like this:
(In this case we have the xml string of the given example)
[type: 'element',tagName: '?xml',attributes:version: '1.0',encoding: 'UTF-8'>,childNodes: [],innerXML: '>',closing: false,closingChar: '?'>,type: 'element',tagName: 'root',attributes: >,childNodes: [type: 'text',text: 'Root Element'>],innerXML: 'Root Element',closing: true,closingChar: null>]
The root object is always an array because of the fact that it handles invalid xml with more than one root element.
There are two kinds of objects. element and text. An object has always the property type . The other keys depend from this type.
type: [String], // "element"tagName: [String], // The tag name of the tagattributes: [Object], // Object containing attributes as propertieschildNodes: [Array], // Array containing child nodes as object nodes ("element" or "text")innerXML: [String], // The inner XML of the tagclosing: [Boolean], // If the tag is closed typically ()closingChar: [String] || null // If it is not closed typically, the char that is used to close it ("!" or "?")>
type: [String], // "text"text: [String] // Text contents of the text node>
The stringifier is the simplest component. Just pass a parsed object structure.
const xml = require("xml-parse");var xmlDoc = [type: 'element',tagName: '?xml',attributes:version: '1.0',encoding: 'UTF-8'>,childNodes: [],innerXML: '>',closing: false,closingChar: '?'>,type: 'element',tagName: 'root',attributes: >,childNodes: [type: 'text',text: 'Root Element'>],innerXML: 'Root Element',closing: true,closingChar: null>]var xmlStr = xml.stringify(xmlDoc, 2); // 2 spacesconsole.log(xmlStr);
The DOM method of xml-parser instance returns a Document-Object-Model with a few methods. It is oriented on the official W3 DOM but not complex as the original.
const xml = require("xml-parse");var xmlDoc = new xml.DOM(xml.parse(' ' +'Root Element')); // Can also be a file path.xmlDoc.document; // Document Object. (Root)
// An element (e.g the 'document' object) has the following prototype methods and properties:var objectNode = document.childNodes[1]; // Just an example// This is the return of a object node elementobjectNode =type: 'element',tagName: 'tagName',attributes: [Object],childNodes: [Object],innerXML: 'innerXML',closing: true,closingChar: null,getElementsByTagName: [Function], // Returns all child nodes with a specific tagNamegetElementsByAttribute: [Function], // Returns all child nodes with a specific attribute valueremoveChild: [Function], // Removes a child nodeappendChild: [Function], // Appends a child nodeinsertBefore: [Function], // Inserts a child nodegetElementsByCheckFunction: [Function], // Returns all child nodes that are validated by validation functionparentNode: [Circular] // Parent Node>
Handling with child nodes
With appendChild or insertBefore methods of every object node, you are allowed to append a child node. You do not have to do something like createElement .
Because a child node is just an object literal, with some properties like type , tagName , attributes and more you just have to pass such an object to the function.
element.appendChild(childNode); // ChildNode is just a object node
const xml = require('xml-parse');var xmlDoc = new xml.DOM(xml.parse(' ' +'Root Element'));var root = xmlDoc.document.getElementsByTagName("root")[0];root.appendChild(type: "element",tagName: "appendedElement",childNodes: [type: "text",text: "Hello World :) I'm appended!">]>);
element.insertBefore(childNode, elementAfter); // ChildNode is just an object literal, 'elementAfter' is just a child node of the parent element
const xml = require('xml-parse');var xmlDoc = new xml.DOM(xml.parse(' ' +'Root Element'));var root = xmlDoc.document.getElementsByTagName("root")[0];root.insertBefore(type: "element",tagName: "insertedElement",childNodes: [type: "text",text: "Hello World :) I'm appended!">]>, root.childNodes[0]);
element.removeChild(childNode); // 'childNode' is just a children of the parent element ('element')
const xml = require('xml-parse');var xmlDoc = new xml.DOM(xml.parse(' ' +'Root Element'));var root = xmlDoc.document.getElementsByTagName("root")[0];root.removeChild(root.childNodes[0]);
The parentNode of a object node represents its parent element. It’s a [Circular] reference.
const xml = require('xml-parse');var xmlDoc = new xml.DOM(xml.parse(' ' +'Root Element'));var root = xmlDoc.document.getElementsByTagName("root")[0];console.log(root.childNodes[0].parentNode); // Returns the 'root' element
element.getElementsByTagName("myTagName"); // Returns all elements whose tag name is 'myTagName'
element.getElementsByAttribute("myAttribute", "myAttributeValue"); // Returns all elements whose attribute 'myAttribute' is 'myAttributeValue'
// With this method you can set custom 'get' methods.element.getElementsByCheckFunction(function(element)if (element.type === "element" && element.childNodes.length == 30)return true;>>); // Returns all elements that have exactly 30 childNodes
Parsing and serializing XML
At times, you may need to parse XML content and convert it into a DOM tree, or, conversely, serialize an existing DOM tree into XML. In this article, we’ll look at the objects provided by the web platform to make the common tasks of serializing and parsing XML easy.
Serializes DOM trees, converting them into strings containing XML.
Constructs a DOM tree by parsing a string containing XML, returning a XMLDocument or Document as appropriate based on the input data.
Loads content from a URL; XML content is returned as an XML Document object with a DOM tree built from the XML itself.
A technology for creating strings that contain addresses for specific portions of an XML document, and locating XML nodes based on those addresses.
Creating an XML document
Using one of the following approaches to create an XML document (which is an instance of Document ).
Parsing strings into DOM trees
This example converts an XML fragment in a string into a DOM tree using a DOMParser :
const xmlStr = 'hey!'; const parser = new DOMParser(); const doc = parser.parseFromString(xmlStr, "application/xml"); // print the name of the root element or error message const errorNode = doc.querySelector("parsererror"); if (errorNode) console.log("error while parsing"); > else console.log(doc.documentElement.nodeName); >
Parsing URL-addressable resources into DOM trees
Using XMLHttpRequest
Here is sample code that reads and parses a URL-addressable XML file into a DOM tree:
const xhr = new XMLHttpRequest(); xhr.onload = () => dump(xhr.responseXML.documentElement.nodeName); >; xhr.onerror = () => dump("Error while getting XML."); >; xhr.open("GET", "example.xml"); xhr.responseType = "document"; xhr.send();
The value in the xhr object’s responseXML field is a Document constructed by parsing the XML.
If the document is HTML, the code shown above will return a Document . If the document is XML, the resulting object is actually a XMLDocument . The two types are essentially the same; the difference is largely historical, although differentiating has some practical benefits as well.
Note: There is in fact an HTMLDocument interface as well, but it is not necessarily an independent type. In some browsers it is, while in others it is an alias for the Document interface.
Serializing an XML document
Given a Document , you can serialize the document’s DOM tree back into XML using the XMLSerializer.serializeToString() method.
Use the following approaches to serialize the contents of the XML document you created in the previous section.
Serializing DOM trees to strings
First, create a DOM tree as described in How to Create a DOM tree. Alternatively, use a DOM tree obtained from XMLHttpRequest .
To serialize the DOM tree doc into XML text, call XMLSerializer.serializeToString() :
const serializer = new XMLSerializer(); const xmlStr = serializer.serializeToString(doc);
Serializing HTML documents
If the DOM you have is an HTML document, you can serialize using serializeToString() , but there is a simpler option: just use the Element.innerHTML property (if you want just the descendants of the specified node) or the Element.outerHTML property if you want the node and all its descendants.
const docInnerHtml = document.documentElement.innerHTML;
As a result, docInnerHtml is a string containing the HTML of the contents of the document; that is, the element’s contents.
You can get HTML corresponding to the and its descendants with this code:
const docOuterHtml = document.documentElement.outerHTML;
See also
Found a content problem with this page?
This page was last modified on Jul 4, 2023 by MDN contributors.
Your blueprint for a better internet.
MDN
Support
Our communities
Developers
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998– 2023 by individual mozilla.org contributors. Content available under a Creative Commons license.