Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here is a quick DOM example that shows how to read and write a simple xml file with its dtd:</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="no"?&gt; &lt;!DOCTYPE roles SYSTEM "roles.dtd"&gt; &lt;roles&gt; &lt;role1&gt;User&lt;/role1&gt; &lt;role2&gt;Author&lt;/role2&gt; &lt;role3&gt;Admin&lt;/role3&gt; &lt;role4/&gt; &lt;/roles&gt; </code></pre> <p>and the dtd:</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;!ELEMENT roles (role1,role2,role3,role4)&gt; &lt;!ELEMENT role1 (#PCDATA)&gt; &lt;!ELEMENT role2 (#PCDATA)&gt; &lt;!ELEMENT role3 (#PCDATA)&gt; &lt;!ELEMENT role4 (#PCDATA)&gt; </code></pre> <p>First import these:</p> <pre><code>import javax.xml.parsers.*; import javax.xml.transform.*; import javax.xml.transform.dom.*; import javax.xml.transform.stream.*; import org.xml.sax.*; import org.w3c.dom.*; </code></pre> <p>Here are a few variables you will need:</p> <pre><code>private String role1 = null; private String role2 = null; private String role3 = null; private String role4 = null; private ArrayList&lt;String&gt; rolev; </code></pre> <p>Here is a reader (String xml is the name of your xml file):</p> <pre><code>public boolean readXML(String xml) { rolev = new ArrayList&lt;String&gt;(); Document dom; // Make an instance of the DocumentBuilderFactory DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); try { // use the factory to take an instance of the document builder DocumentBuilder db = dbf.newDocumentBuilder(); // parse using the builder to get the DOM mapping of the // XML file dom = db.parse(xml); Element doc = dom.getDocumentElement(); role1 = getTextValue(role1, doc, "role1"); if (role1 != null) { if (!role1.isEmpty()) rolev.add(role1); } role2 = getTextValue(role2, doc, "role2"); if (role2 != null) { if (!role2.isEmpty()) rolev.add(role2); } role3 = getTextValue(role3, doc, "role3"); if (role3 != null) { if (!role3.isEmpty()) rolev.add(role3); } role4 = getTextValue(role4, doc, "role4"); if ( role4 != null) { if (!role4.isEmpty()) rolev.add(role4); } return true; } catch (ParserConfigurationException pce) { System.out.println(pce.getMessage()); } catch (SAXException se) { System.out.println(se.getMessage()); } catch (IOException ioe) { System.err.println(ioe.getMessage()); } return false; } </code></pre> <p>And here a writer:</p> <pre><code>public void saveToXML(String xml) { Document dom; Element e = null; // instance of a DocumentBuilderFactory DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); try { // use factory to get an instance of document builder DocumentBuilder db = dbf.newDocumentBuilder(); // create instance of DOM dom = db.newDocument(); // create the root element Element rootEle = dom.createElement("roles"); // create data elements and place them under root e = dom.createElement("role1"); e.appendChild(dom.createTextNode(role1)); rootEle.appendChild(e); e = dom.createElement("role2"); e.appendChild(dom.createTextNode(role2)); rootEle.appendChild(e); e = dom.createElement("role3"); e.appendChild(dom.createTextNode(role3)); rootEle.appendChild(e); e = dom.createElement("role4"); e.appendChild(dom.createTextNode(role4)); rootEle.appendChild(e); dom.appendChild(rootEle); try { Transformer tr = TransformerFactory.newInstance().newTransformer(); tr.setOutputProperty(OutputKeys.INDENT, "yes"); tr.setOutputProperty(OutputKeys.METHOD, "xml"); tr.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); tr.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "roles.dtd"); tr.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); // send DOM to file tr.transform(new DOMSource(dom), new StreamResult(new FileOutputStream(xml))); } catch (TransformerException te) { System.out.println(te.getMessage()); } catch (IOException ioe) { System.out.println(ioe.getMessage()); } } catch (ParserConfigurationException pce) { System.out.println("UsersXML: Error trying to instantiate DocumentBuilder " + pce); } } </code></pre> <p>getTextValue is here:</p> <pre><code>private String getTextValue(String def, Element doc, String tag) { String value = def; NodeList nl; nl = doc.getElementsByTagName(tag); if (nl.getLength() &gt; 0 &amp;&amp; nl.item(0).hasChildNodes()) { value = nl.item(0).getFirstChild().getNodeValue(); } return value; } </code></pre> <p>Add a few accessors and mutators and you are done!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload