Hallo!
Ich bin mit meinem Problem immer noch nicht weiter. Ich habe in einer xml-datei tags gelöscht und speichere die datei wieder ab. das problem ist nur, dass er die tags zwar löscht, aber die zeilen bleiben da. also hab ich ganz viele leerzeieln in meiner xml-datei und zum bearbeiten ist das net sehr übersichtlich. kann mir da jmd einen tipp geben? such schon ein paar tage nach einer lösung und komm net weiter.
hier mal mein quellcode:
public class Unbekannt1 {
/* ***** Document vorbereiten ***** */
Document doc = new DocumentImpl();
Document document_xml = null;
String str_slash = "\";
public void readXML (String str_CSVpfad,
String str_XMLpfad,
String str_Abb_XMLpfad) {
XPathAPI xp = new XPathAPI();
DocumentBuilderFactory dfactory = null;
Properties prop = null;
String str_value = "", str_CSV_zeile = "";
String str_NR = "", str_LCN = "";
String str_abNR = "", str_dateiname = "";
NodeList nodelist_item = null, nodelist_childnodes_item = null;
Element node_doktype = null;
Text node_inhalt = 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));
NodeList nodelist_child_item = nodelist_item.item(i).getChildNodes();
}
}
}
System.out.println("lange "+(xp.selectNodeList(document_xml.getDocumentElement(),"//doktype")).getLength());
for (int i = 0; i < nodelist_item.getLength(); i++) {
str_value = nodelist_item.item(i).getAttributes().getNamedItem("lcn").getNodeValue();
try {
file_CSV = new RandomAccessFile (str_CSVpfad, "r");
/* ***** Zeilenweises Auslesen der Begriffsdatei ***** */
long_length = file_CSV.length();
while( file_CSV.getFilePointer() != long_length) {
str_CSV_zeile = file_CSV.readLine();
int int_pos_end_NR = str_CSV_zeile.indexOf(";");
if (int_pos_end_NR != -1) {
str_NR = str_CSV_zeile.substring (0, int_pos_end_NR);
str_LCN = str_NR.substring (17, 25);
str_abNR = str_CSV_zeile.substring (int_pos_end_NR+1, str_CSV_zeile.length());
int int_pos_end_dateiname = str_abNR.indexOf(";");
if (int_pos_end_dateiname != -1) {
str_dateiname = str_abNR.substring(0, int_pos_end_dateiname);
}
if (str_LCN.equals(str_value) == true) {
node_doktype = document_xml.createElement("doktype");
node_inhalt = document_xml.createTextNode(str_dateiname);
node_doktype.appendChild(node_inhalt);
nodelist_item.item(i).appendChild(node_doktype);
}
}
}
}catch (IOException ioe) {
System.out.println(ioe.getMessage());
}
}
/* ***** 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 echt super, wnen ihr mir weiterhelfen könntet.
LG Kai