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, ∼2192🔥, 0💬
Popular Posts:
How to download and install Apache XMLBeans-2.6.0.zip? If you want to try the XMLBeans Java library,...
JDK 11 jdk.crypto.mscapi.jmod is the JMOD file for JDK 11 Crypto MSCAPI module. JDK 11 Crypto MSCAPI...
What JAR files are required to run dom\Writer.java provided in the Apache Xerces package? 3 JAR file...
JDK 11 java.xml.jmod is the JMOD file for JDK 11 XML (eXtensible Markup Language) module. JDK 11 XML...
What Is in Xerces-J-bin.2.12.2.zip? Xerces-J-bin.2.12.2.zip file is the distribution package ZIP fil...