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, ∼2738🔥, 0💬
Popular Posts:
Where Can I get source code files of jsse.jar? You can get source code files of jsse.jar (JSSE) from...
commons-lang-2.6.jar is the JAR file for Apache Commons Lang 2.6, which provides a host of helper ut...
Jetty provides an HTTP server, HTTP client, and javax.servlet container. These components are open s...
What Is in Xerces-J-bin.2.12.2.zip? Xerces-J-bin.2.12.2.zip file is the distribution package ZIP fil...
JDK 11 java.management.jmod is the JMOD file for JDK 11 Management module. JDK 11 Management module ...