modulexml
xml processing module, you can use the xml module to parse and process xml and html files
To parse the xml file you can use code like this:
1
2
3
4
5
6
7var xml = require('xml');
var fs = require('fs');
var xmlStr = fs.readFile('test.xml');
var xmlDoc = xml.parse(xmlStr);
console.log(xmlDoc.documentElement.nodeName); // output root node name
In the above code we usefsThe module's readFile method reads an xml file, then uses the xml module's parse method to parse the xml file and returns anXmlDocumentObject xmlDoc. Then, we can access the root element of the xml document through xmlDoc.documentElement.
To parse html files, you just need to modify your code slightly:
1
2
3
4
5
6
7var xml = require('xml');
var fs = require('fs');
var htmlStr = fs.readFile('test.html');
var xmlDoc = xml.parse(htmlStr, 'text/html');
console.log(xmlDoc.documentElement.nodeName); // output root node name
Here we also usefsThe readFile method of the module reads an html file, but when we call the parse method of the xml module, we specify the second parameter as 'text/html', so that the xml module will parse the file according to the grammatical rules of html.
The parsed Xml document objects areXmlDocumentTypes, their properties and methods can all be manipulated with reference to the XML Object Model (DOM).
object
Document
xml document object, seeXmlDocumentobject
1XmlDocument xml.Document;
static function
parse
Parse xml/html text and createXmlDocumentObject, multilingual is not supported
1
2static XmlDocument xml.parse(String source,
String type = "text/xml");
Call parameters:
- source: String, specifies the xml/html text that needs to be parsed
- type: String, specifies the text type, the default is text/xml, it can also be specified as text/html
Return results:
- XmlDocument, returns the createdXmlDocumentobject
Parse xml/html and createXmlDocumentObject, which will be converted according to the specified language during parsing
1
2static XmlDocument xml.parse(Buffer source,
String type = "text/xml");
Call parameters:
- source:Buffer, specify the xml/html binary data that needs to be parsed
- type: String, specifies the text type, the default is text/xml, it can also be specified as text/html
Return results:
- XmlDocument, returns the createdXmlDocumentobject
serialize
SerializationXmlNodeis a string
1static String xml.serialize(XmlNode node);
Call parameters:
Return results:
- String, returns the serialized string
constant
ELEMENT_NODE
XmlNodeThe nodeType attribute constant indicates that the node isXmlElementobject
1const xml.ELEMENT_NODE = 1;
ATTRIBUTE_NODE
XmlNodeThe nodeType attribute constant indicates that the node isXmlAttrobject
1const xml.ATTRIBUTE_NODE = 2;
TEXT_NODE
XmlNodeThe nodeType attribute constant indicates that the node isXmlTextobject
1const xml.TEXT_NODE = 3;
CDATA_SECTION_NODE
XmlNodeThe nodeType attribute constant indicates that the node isXmlCDATASectionobject
1const xml.CDATA_SECTION_NODE = 4;
PROCESSING_INSTRUCTION_NODE
XmlNodeThe nodeType attribute constant indicates that the node isXmlProcessingInstructionobject
1const xml.PROCESSING_INSTRUCTION_NODE = 7;
COMMENT_NODE
XmlNodeThe nodeType attribute constant indicates that the node isXmlCommentobject
1const xml.COMMENT_NODE = 8;
DOCUMENT_NODE
XmlNodeThe nodeType attribute constant indicates that the node isXmlDocumentobject
1const xml.DOCUMENT_NODE = 9;
DOCUMENT_TYPE_NODE
XmlNodeThe nodeType attribute constant indicates that the node isXmlDocumentTypeobject
1const xml.DOCUMENT_TYPE_NODE = 10;