Writing /volume1/Web/Public/dokuwiki/data/log/deprecated/2024-11-15.log failed

文書の過去の版を表示しています。


Writing /volume1/Web/Public/dokuwiki/data/log/deprecated/2024-11-15.log failed

Geneate xml with dom

Dom level 3を用いたXML生成方法を紹介する。

Example

例として、次の簡単なxmlを作成してみよう。

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE USERS SYSTEM "users.dtd">
<USERS>
  <USER ID="PWD122" TYPE="customer">Tim@Home</USER>
  <USER ID="MX787" TYPE="manager">Jack&amp;Moud</USER>
  <USER ID="A4Q45" TYPE="employee">John D&apos;</USER>
</USERS>

source snippet

以下にソースの一部分を示す。

     public static void generateXmlWithDom(OutputStream out) throws Exception {
 
		DOMImplementation impl = DOMImplementationImpl.getDOMImplementation();
		//set DOCTYPE ( dtd )
		DocumentType doctype = impl.createDocumentType("users", null, "users.dtd");
		Document xmldoc = impl.createDocument(null, null, doctype);
 
		// Root element.
		// Root element.
	      Element root = xmldoc.createElement("USERS");
	      String[] id = {"PWD122","MX787","A4Q45"};
	      String[] type = {"customer","manager","employee"};
	      String[] desc = {"Tim@Home","Jack&Moud","John D'o"};
	      for (int i=0;i<id.length;i++)
	      {
			// Child i.
			e = xmldoc.createElementNS(null, "USER");
			e.setAttributeNS(null, "ID", id[i]);
			e.setAttributeNS(null, "TYPE", type[i]);
			n = xmldoc.createTextNode(desc[i]);
			e.appendChild(n);
			root.appendChild(e);
	      }
 
		xmldoc.appendChild(root);
 
		// Xml serialization
		DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
		DOMImplementationLS ls = (DOMImplementationLS) registry.getDOMImplementation("LS");
 
		LSSerializer writer = ls.createLSSerializer();
		LSOutput output = ls.createLSOutput();
		output.setByteStream(out);
		output.setEncoding("UTF-8");
		// set indent
		DOMConfiguration config = writer.getDomConfig();
		config.setParameter("format-pretty-print", new Boolean(true));
 
		writer.write(xmldoc, output);
 
		out.close();
 
 
       }// generateXmlWithDom

reference


QR Code
QR Code study:java:xml:create_xml (generated for current page)