Categories:
Audio (13)
Biotech (29)
Bytecode (36)
Database (77)
Framework (7)
Game (7)
General (507)
Graphics (53)
I/O (35)
IDE (2)
JAR Tools (101)
JavaBeans (21)
JDBC (121)
JDK (426)
JSP (20)
Logging (108)
Mail (58)
Messaging (8)
Network (84)
PDF (97)
Report (7)
Scripting (84)
Security (32)
Server (121)
Servlet (26)
SOAP (24)
Testing (54)
Web (15)
XML (309)
Collections:
Other Resources:
DomXmlSerializer.java - Serialize DOM to XML String
How to serialize a DOM object to an XML string?
✍: FYIcenter
If you want to serialize (or convert) a DOM object to an XML string, you can follow these suggestions:
1. Build your DOM document object with the DOM API:
DocumentBuilder b = f.newDocumentBuilder(); Document d = b.newDocument(); Element e = d.createElement("User"); d.appendChild(e); ...
2. Call the getDOMImplementation() method on the DocumentBuilder object to figure out DOM LS (Load and Save) impletation object. Then use it to create the LSSerializer object.
DOMImplementationLS ls = (DOMImplementationLS) b.getDOMImplementation(); LSSerializer s = ls.createLSSerializer(); System.out.println("DOMImplementationLS class:\n "+ls.getClass().getName()); System.out.println("LSSerializer class:\n "+s.getClass().getName());
3. Call writeToString() method on the LSSerializer object to convert DOM object to an XML string:
System.out.println("XML from LSSerializer:\n"+s.writeToString(d));
4. Call getDomConfig() method on the LSSerializer object to retrieve the DOMCongiguratin object. Then set various parameters to control the LSSerializer
System.out.println("XML from LSSerializer:\n"+s.writeToString(d)); DOMConfiguration p = s.getDomConfig(); p.setParameter("format-pretty-print",Boolean.TRUE); System.out.println("Pretty XML from LSSerializer:\n"+s.writeToString(d));
Here is a complete example of parsing an XML file with DOM API, DomXmlSerializer.java:
// Copyright (c) 2017 FYIcenter.com import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilder; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Text; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.w3c.dom.DOMConfiguration; import org.w3c.dom.ls.DOMImplementationLS; import org.w3c.dom.ls.LSSerializer; public class DomXmlSerializer { public static void main(String[] args) throws Exception { DocumentBuilderFactory f = DocumentBuilderFactory.newInstance(); DocumentBuilder b = f.newDocumentBuilder(); Document d = b.newDocument(); Element e = d.createElement("User"); d.appendChild(e); Element c = d.createElement("ID"); c.appendChild(d.createTextNode("202")); e.appendChild(c); c = d.createElement("Name"); c.appendChild(d.createTextNode("Frank Y. Ivy")); e.appendChild(c); c = d.createElement("BirthDate"); c.appendChild(d.createTextNode("1970-01-01")); e.appendChild(c); DOMImplementationLS ls = (DOMImplementationLS) b.getDOMImplementation(); LSSerializer s = ls.createLSSerializer(); System.out.println("DOMImplementationLS class:\n "+ls.getClass().getName()); System.out.println("LSSerializer class:\n "+s.getClass().getName()); System.out.println("XML from LSSerializer:\n"+s.writeToString(d)); DOMConfiguration p = s.getDomConfig(); p.setParameter("format-pretty-print",Boolean.TRUE); System.out.println("Pretty XML from LSSerializer:\n"+s.writeToString(d)); } }
Compile and run the example program, DomXmlSerializer.java:
>\fyicenter\jdk-1.8.0\bin\javac DomXmlParserWhitespace.java >\fyicenter\jdk-1.8.0\bin\java DomXmlParserWhitespace UserDTD.xml false Implementation class: com.sun.org.apache.xerces.internal.dom.DeferredDocumentImpl DOM object elements and text contents: .User: ..#text:
⇒ DomXmlDtdValidation.java - DOM Parser with DTD Validation
⇐ DomXmlParserWhitespace.java - Parse XML File without Whitespaces
2017-12-13, 1855🔥, 0💬
Popular Posts:
Apache Log4j 1.2 Bridge allows applications coded to use Log4j 1.2 API to use Log4j 2 instead. Bytec...
JDK 1.1 source code directory contains Java source code for JDK 1.1 core classes: "C:\fyicenter\jdk-...
Java Architecture for XML Binding (JAXB) is a Java API that allows Java developers to map Java class...
JDK 11 jdk.jshell.jmod is the JMOD file for JDK 11 JShell tool, which can be invoked by the "jshell"...
Apache Log4j Core Implementation provides the functional components of the logging system. Users are...