Conversion code of string xml in JavaScript

Source: Internet
Author: User

> During Normal XML data exchange, I prefer to pass XML strings instead of formatted XML documents. This involves the conversion of XML strings and Xml documents. To put it bluntly, this is a very simple problem. This article lists the various XML Parser as follows to facilitate future reference.

1. Use the original javax. xml. parsers, standard jdk api

// Convert string to XML
String xmlStr = \"......\";
StringReader sr = new StringReader (xmlStr );
InputSource is = new InputSource (sr );
DocumentBuilderFactory factory = DocumentBuilderFactory. newInstance ();
DocumentBuilder builder = factory. newDocumentBuilder ();
Document doc = builder. parse (is );

// Convert XML to string
TransformerFactory tf = TransformerFactory. newInstance ();
Transformer t = tf. newTransformer ();
T. setOutputProperty (\ "encoding \", \ "GB23121 \"); // solve Chinese problems. If you have tried GBK, it will not work.
ByteArrayOutputStream bos = new ByteArrayOutputStream ();
T. transform (new DOMSource (doc), new StreamResult (bos ));
String xmlStr = bos. toString ();

The xml document here is org. w3c. dom. Document

2. The program becomes simpler after dom4j is used

// Convert string to XML
String xmlStr = \"......\";
Document document = incluenthelper. parseText (xmlStr );

// Convert XML to string
Document document = ...;
String text = document. asXML ();

Here the xml document is org. dom4j. Document

3. Use JDOM

The JDOM processing method is very similar to the first method.

// Convert string to XML
String xmlStr = \".....\";
StringReader sr = new StringReader (xmlStr );
InputSource is = new InputSource (sr );
Document doc = (new SAXBuilder (). build (is );

// Convert XML to string
Format format = Format. getPrettyFormat ();
Format. setEncoding (\ "gb2312 \"); // set the character of the xml file to gb2312 to solve the Chinese problem.
XMLOutputter xmlout = new XMLOutputter (format );
ByteArrayOutputStream bo = new ByteArrayOutputStream ();
Xmlout. output (doc, bo );
String xmlStr = bo. toString ();

Here the xml document is org. jdom. Document

Iv. Processing in JAVASCRIPT

// Convert string to XML
Var xmlStr = \".....\";
Var xmlDoc = new ActiveXObject (\ "Microsoft. XMLDOM \");
XmlDoc. async = false;
XmlDoc. loadXML (xmlStr );
// You can process the xmlDoc.
Var name = xmlDoc. selectSingleNode (\ "/person/name \");
Alert (name. text );

// Convert XML to string
Var xmlDoc = ......;
Var xmlStr = xmlDoc. xml

Here, the xml document is the xml dom of the javascript version.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.