Hi!
Ich hoffe mir kann jmd weiterhelfen. Ich programmiere in java und möchte eine xml-datei verändern. hierzu öffne ich die xml-datei, suche die doktype-knoten und lösche diese mit der FUnktion removeChild. Das Problem ist nur, sie werden zwar gelöscht, aber die Leerzeilen bleiben in der Datei stehn.
Hier mal der Quelltext:
public void readXML (String str_CSVpfad,
String str_XMLpfad,
String str_Abb_XMLpfad) {
XPathAPI xp = new XPathAPI();
Document document_xml = null;
DocumentBuilderFactory dfactory = null;
Properties prop = null;
String str_lcn_value = "", str_CSV_zeile = "";
String str_cobraNR = "", str_cobraLCN = "";
NodeList nodelist_item = null, nodelist_childnodes_item = null;
Element node_doktype = null;
java.io.FileInputStream file = null;
FileWriter write_file;
RandomAccessFile file_CSV;
long long_length ;
/* ***** Properties für XML Ausgabe festlegen ***** */
prop = OutputProperties.getDefaultMethodProperties("xml");
prop.setProperty("encoding","iso-8859-1");
try {
dfactory = DocumentBuilderFactory.newInstance();
document_xml = dfactory.newDocumentBuilder().parse(str_XMLpfad);
if ((nodelist_item = xp.selectNodeList(document_xml.getDocumentElement(),"//item")) != null) {
for (int i = 0; i < nodelist_item.getLength(); i++) {
//Löschen der doktypes aud der item-liste
nodelist_childnodes_item = nodelist_item.item(i).getChildNodes();
for (int j = 0; j < nodelist_childnodes_item.getLength(); j++) {
if (nodelist_childnodes_item.item(j).getNodeName() == "doktype") {
nodelist_item.item(i).removeChild(nodelist_childnodes_item.item(j));
}
}
}
System.out.println("lange "+(xp.selectNodeList(document_xml.getDocumentElement(),"//doktype")).getLength());
}
/* ***** Ausgabe als formatierte XML-Datei ***** */
Serializer serializer = SerializerFactory.getSerializer(prop);
OutputStream out=new FileOutputStream(str_Abb_XMLpfad);
serializer.setOutputStream(out);
serializer.asDOMSerializer().serialize(document_xml);
} catch( ParserConfigurationException pce ) {
System.out.println("ParserConfigurationException");
} catch( SAXException se ) {
System.out.println(se.getMessage()+"SAXException beim Lesen");
} catch( IOException ioe) {
System.out.println("IOException");
} catch (javax.xml.transform.TransformerException tre) {
System.out.println("TransformerException");
}
}
Wäre super, wenn ihr mir einen Tipp geben könntet.
LG Katti