Skip to content

informat.xml XML Operations

Overview

Use informat.xml to perform XML read and write operations

parse

Read XML content and return a Document object. If the XML content is null or an empty string, null will be returned. If the content structure is not a valid XML format, an exception will be thrown.

javascript
informat.xml.parse(xml);
ParameterTypeDescription
xmlStringXML content

Return Value Type: org.w3c.dom.Document

About w3c Document API methods, please refer to: https://docs.oracle.com/javase/7/docs/api/org/w3c/dom/Document.html

Here is an example:

js
let doc = informat.xml.parse("<root name='test'>text content</root>");
let name = doc.getDocumentElement().getAttribute("name");
console.log(name); //test

createDocument

Create a new Document object

javascript
informat.xml.createDocument();

Return Value Type: org.w3c.dom.Document

About w3c Document API methods, please refer to: https://docs.oracle.com/javase/7/docs/api/org/w3c/dom/Document.html

doc2xml

Convert a Document object to an XML string

javascript
informat.xml.doc2xml(doc, config);
ParameterTypeDescription
docDocumentXML document object
configConfigOutput configuration

config object result:

{
    doctypeSystem:String,//doctype system value
    doctypePublic:String,//doctype public value
    indent:Boolean,//defaults to false, whether to indent XML
    indentAmount:Integer,//indentation length, defaults to 0
    standalone:Boolean,//standalone value in the XML declaration
    version:String,//version value in the XML declaration
    encoding:String,//encoding value in the XML declaration
    omitXmlDeclaration:Boolean,//whether to omit the XML declaration
}

Return Value Type: String

Here is an example:

js
let doc = informat.xml.createDocument();
let rootElement = doc.createElement("company");
doc.appendChild(rootElement);
doc.createElement("staff");
rootElement.appendChild(doc.createElement("staff"));
//
let config = {};
config.doctypeSystem = "doctype system";
config.doctypePublic = "doctype public";
config.indent = true;
config.indentAmount=2;
config.standalone = false;
config.version = "1.1";
config.encoding = "utf-8";
//
let xml = informat.xml.doc2xml(doc, config);
console.log(xml);
xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE company PUBLIC "doctype public" "doctype system">
<company>
    <staff/>
</company>

If config is set as follows:

config.omitXmlDeclaration=true;
config.indent=true;

The output result will be:

xml
<!DOCTYPE company PUBLIC "doctype public" "doctype system">
<company>
    <staff/>
</company>