public static void main(String argv[]) { File file = new File("xml1.xml"); XmlReader reader; XmlWriter writer; XmlDocument document; String xmlout; // Construct a document and parse it System.out.println("We are going to parse from " + file + "."); System.out.println("The parser will be set to verbose mode\n"); document = new XmlDocument(file); reader = new XmlReader(); reader.setVerbose(true); try { reader.parse(document); } catch (Exception e) { e.printStackTrace(); } int errors = reader.getErrorCount(); int warnings = reader.getWarningCount(); System.err.println("Completed: " + errors + " errors, " + warnings + " warnings"); if (errors > 0) { System.err.println("Unrecoverable errors in XML. Stop."); return; } // Now print it System.out.println("\nHaving parsed the document, we will print it out."); System.out.println("Here is the DTD:\n"); writer = new XmlWriter(); Writer out = new OutputStreamWriter(System.out); try { writer.writeDTD(document, out); } catch (Exception e) { e.printStackTrace(); } System.out.println("\nHere is the XML:\n"); try { writer.write(document, out); } catch (Exception e) { e.printStackTrace(); } }
public void write(XmlVisitor xa) { ElementVisitor elementVisitor = xa.visitElement(name); // Attributes if (attributes != null) { for (Entry<String, TextWriter> entry : attributes.entrySet()) { String attrName = entry.getKey(); TextWriter textWriter = entry.getValue(); String value = textWriter.getText(); if (value != null) elementVisitor.visitAttribute(attrName, value); } } // Body if (body != null && !body.isEmpty()) { XmlVisitor bodyVisitor = elementVisitor.visitBody(); body.write(bodyVisitor); bodyVisitor.visitEndBody(); } elementVisitor.visitEndElement(); }
public void write(XmlVisitor xa) { for (XmlWriter child : children) child.write(xa); }