Categories:
Audio (13)
Biotech (29)
Bytecode (36)
Database (77)
Framework (7)
Game (7)
General (507)
Graphics (53)
I/O (35)
IDE (2)
JAR Tools (102)
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 (322)
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, ∼2359🔥, 0💬
Popular Posts:
What is the jaxp\TypeInfoWriter.java provided in the Apache Xerces package? I have Apache Xerces 2.1...
What Is fop.jar? I got it from the fop-2.7-bin.zip. fop.jar in fop-2.7-bin.zip is the JAR file for F...
JAX-RPC is an API for building Web services and clients that used remote procedure calls (RPC) and X...
JDK 17 jdk.jfr.jmod is the JMOD file for JDK 17 JFR module. JDK 17 JFR module compiled class files a...
Apache Log4j 1.2 Bridge allows applications coded to use Log4j 1.2 API to use Log4j 2 instead. Bytec...