You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-dev@xerces.apache.org by Nick Sullivan <ni...@bikestore.com> on 2000/07/29 02:12:41 UTC
Indenting
I am writing an application that parses both raw (tab delimited)
and XML files, puts them into the DOM, and then optionally writes
the result to System.out or an output file.
My problem is with indenting... There seems to be an issue depending on
how the DOM is generated. If I set the indenting to where it works with
the raw data, it breaks the XML, and vice versa. By "breaks" I mean
that the indenting is either non-existent or malformed.
This produces the proper indenting with the RAW data
StringWriter stringOut = new StringWriter();
OutputFormat format = new OutputFormat(doc); //Serialize DOM
format.setIndent(4);
format.setLineWidth(0);
XMLSerializer serial = new XMLSerializer (stringOut, format);
serial.asDOMSerializer(); // As a DOM Serializer
serial.serialize(doc.getDocumentElement());
System.out.println( "STRXML = " + stringOut.toString() );
==========================
This produces the proper indenting with the XML file generated DOM
StringWriter stringOut = new StringWriter();
OutputFormat format = new OutputFormat(doc, "UTF-8", true); //Serialize
DOM
format.setPreserveSpace(true);
XMLSerializer serial = new XMLSerializer (stringOut, format);
serial.asDOMSerializer(); // As a DOM Serializer
serial.serialize(doc.getDocumentElement());
System.out.println( "STRXML = " + stringOut.toString() );
But neither one works for BOTH the raw data and the XML file. I could hack
this with an "if" statement depending on how the DOM is generated...
But I was hoping there was a more elegant solution... Anyone?
I thought it might be a problem with how I was generating the DOM
from the raw data, but I took code from the DOMGenerate sample
and it does the same thing...Nevertheless, here is the code for both:
XML file:
InputSource XMLFile = new InputSource(new FileReader(XMLFile));
DOMParser parser = new DOMParser();
parser.parse(XMLData);
Raw data:
//Set up the DOM and create the root element
Document doc= new DocumentImpl();
Element root = doc.createElement("rows");
//Get the field names from the first line
String[] FieldNames = Javortex.Split(Delimiter, in.readLine());
//Walk through the rest of the file and get FieldData
while ((DataLine=in.readLine()) != null) {
Element record = doc.createElement("row");
//Create an attribute for rowNum
record.setAttribute("rowNum", String.valueOf(rowNum++));
//Split and Print the data
String[] FieldData = Javortex.Split(Delimiter, DataLine,
FieldNames.length);
for (i=0; i < FieldNames.length; i++) {
Element column = doc.createElement(FieldNames[i]);
//Only create the TextNode if its not null, so it becomes an empty
tag
if (FieldData[i].length() > 0 )
column.appendChild(doc.createTextNode(FieldData[i]));
record.appendChild(column);
}
root.appendChild(record);
}
// Wrap up
doc.appendChild(root);
BTW, I am using Xerces 1.12...
Nick Sullivan