I am writing a Java program to read in XML file, modify some elements
slightly, and then write it out. That XML file is prepared
in Docbook.
It works fine, except that it is disturbing the carriage returns in places
where they have meaning.
Attached are a sample input file, the sample output file, and a simplified
version of my Java program. My real file, examines certain element's attributes
and adds certain elements to the DOM data structure.
How, best to write such a program, without disturbing the carriage returns
between "<programlistin g>" and "</programlisting> "
Here is the input file. I need to preserve the formatting of the material
between "<programlistin g>" and "</programlisting> "
<test>
<programlisting >
a
b
c d e f
</programlisting>
</test>
Here is the output file -- observe how the "a b c d e f" are now on
one line.
<?xml version="1.0" encoding="UTF-8"?>
<test>
<programlisting > a b c d e f </programlisting>
</test>
Here is the Java program:
import java.text.*;
import java.io.*;
import javax.xml.parse rs.*;
import org.apache.xml. serialize.*;
import org.w3c.dom.*;
import org.xml.sax.*;
public class Test {
static PrintWriter debug = null;
static Document document = null;
static String OutputFileName;
static Element CreateElement (String ElementName, String Contents){
Element ToReturn;
ToReturn = document.create Element(Element Name);
Text T = document.create TextNode(Conten ts);
ToReturn.append Child(T);
return ToReturn;
}
static Element CreateElement (String ElementName, String Contents, String AttributeName, String AttributeValue) {
Element ToReturn = CreateElement(E lementName,Cont ents);
ToReturn.setAtt ribute(Attribut eName,Attribute Value);
return ToReturn;
}
public static void main (String[] args) throws FileNotFoundExc eption {
try {
debug = new PrintWriter (new FileWriter("deb ug.out"));
}
catch (Exception d) {System.out.pri ntln("cannot open debug file");}
Text T;
int j;
DocumentBuilder parser = null;
// Here we read in the data from the XML file
DocumentBuilder Factory Factory = DocumentBuilder Factory.newInst ance();
String xmlFile = args[0];
File file = new File (xmlFile);
try {
parser = Factory.newDocu mentBuilder();
}
catch (ParserConfigur ationException pce) {
System.out.prin tln ("Parser Configuration Exception " + pce.getMessage( ));
System.exit(0);
}
try {
document = parser.parse(fi le);
}
catch (SAXException se) {
System.out.prin tln ("SAX Exception on parsing document " + se.getMessage() );
System.exit(0);
}
catch (IOException ioe) {
System.out.prin tln ("IO Exception on parsing document " + ioe.getMessage( ));
System.exit(0);
}
FileWriter out = null;
XMLSerializer X = null;
Element root = null;
OutputFileName= args[0];
try {
out = new FileWriter(Outp utFileName+".OU T"+".xml");
out.flush();
OutputFormat o = new OutputFormat(do cument);
o.setIndent(5);
o.setIndenting( true);
X = new XMLSerializer(o );
X.setOutputChar Stream(out);
}
catch (IOException e0) {
System.out.prin tln ("problem in setting up to save XML file" + e0.getMessage() );
e0.printStackTr ace();
}
// use the XML functions to dump the materials
try {
X.serialize(doc ument);
out.flush();
} catch (IOException e2) {System.out.pri ntln("error writing file " + e2.getMessage() );e2.printStack Trace();}
debug.close();
}
}
Dr. Laurence Leff Western Illinois University, Macomb IL 61455 ||(309) 298-1315
Stipes 447 Assoc. Prof. of Computer Sci. Pager: 309-367-0787 FAX: 309-298-2302
Secretary: eContracts Technical Committee OASIS Legal XML Member Section
slightly, and then write it out. That XML file is prepared
in Docbook.
It works fine, except that it is disturbing the carriage returns in places
where they have meaning.
Attached are a sample input file, the sample output file, and a simplified
version of my Java program. My real file, examines certain element's attributes
and adds certain elements to the DOM data structure.
How, best to write such a program, without disturbing the carriage returns
between "<programlistin g>" and "</programlisting> "
Here is the input file. I need to preserve the formatting of the material
between "<programlistin g>" and "</programlisting> "
<test>
<programlisting >
a
b
c d e f
</programlisting>
</test>
Here is the output file -- observe how the "a b c d e f" are now on
one line.
<?xml version="1.0" encoding="UTF-8"?>
<test>
<programlisting > a b c d e f </programlisting>
</test>
Here is the Java program:
import java.text.*;
import java.io.*;
import javax.xml.parse rs.*;
import org.apache.xml. serialize.*;
import org.w3c.dom.*;
import org.xml.sax.*;
public class Test {
static PrintWriter debug = null;
static Document document = null;
static String OutputFileName;
static Element CreateElement (String ElementName, String Contents){
Element ToReturn;
ToReturn = document.create Element(Element Name);
Text T = document.create TextNode(Conten ts);
ToReturn.append Child(T);
return ToReturn;
}
static Element CreateElement (String ElementName, String Contents, String AttributeName, String AttributeValue) {
Element ToReturn = CreateElement(E lementName,Cont ents);
ToReturn.setAtt ribute(Attribut eName,Attribute Value);
return ToReturn;
}
public static void main (String[] args) throws FileNotFoundExc eption {
try {
debug = new PrintWriter (new FileWriter("deb ug.out"));
}
catch (Exception d) {System.out.pri ntln("cannot open debug file");}
Text T;
int j;
DocumentBuilder parser = null;
// Here we read in the data from the XML file
DocumentBuilder Factory Factory = DocumentBuilder Factory.newInst ance();
String xmlFile = args[0];
File file = new File (xmlFile);
try {
parser = Factory.newDocu mentBuilder();
}
catch (ParserConfigur ationException pce) {
System.out.prin tln ("Parser Configuration Exception " + pce.getMessage( ));
System.exit(0);
}
try {
document = parser.parse(fi le);
}
catch (SAXException se) {
System.out.prin tln ("SAX Exception on parsing document " + se.getMessage() );
System.exit(0);
}
catch (IOException ioe) {
System.out.prin tln ("IO Exception on parsing document " + ioe.getMessage( ));
System.exit(0);
}
FileWriter out = null;
XMLSerializer X = null;
Element root = null;
OutputFileName= args[0];
try {
out = new FileWriter(Outp utFileName+".OU T"+".xml");
out.flush();
OutputFormat o = new OutputFormat(do cument);
o.setIndent(5);
o.setIndenting( true);
X = new XMLSerializer(o );
X.setOutputChar Stream(out);
}
catch (IOException e0) {
System.out.prin tln ("problem in setting up to save XML file" + e0.getMessage() );
e0.printStackTr ace();
}
// use the XML functions to dump the materials
try {
X.serialize(doc ument);
out.flush();
} catch (IOException e2) {System.out.pri ntln("error writing file " + e2.getMessage() );e2.printStack Trace();}
debug.close();
}
}
Dr. Laurence Leff Western Illinois University, Macomb IL 61455 ||(309) 298-1315
Stipes 447 Assoc. Prof. of Computer Sci. Pager: 309-367-0787 FAX: 309-298-2302
Secretary: eContracts Technical Committee OASIS Legal XML Member Section
Comment