Hallo guten Morgen,
ich möchte aus dieser XML-Datei den Discount-Wert des entsprechenden condtions-Tags auslesen.
Conditions.xml
Dazu habe ich folgende Java-Klasse "ReadConditions.java"
und die beiden Klassen, die importiert werden:
Constants.java:
XMLConditonsCreator.java:
Mein Problem ist, dass diese Konstruktion ein leeres Array zurückliefert, obwohl ein Array mit einem Wert zurückkommen müsste.
Wenn ich die ReadConditions.java mit dem Debugger starte und den Breakpoint bei System.out.println setze, sehe ich dass alles fehlerfrei durchläuft. Nur der Rückgabewert in readXml(String conditionName) results liefert "size=0" und in der For-Schleife ist "elements: null". So wird der Hauptklasse ein leeres Array als Ergebnis übergeben.
Ich sehe im Moment nicht, wo mein Ansatzfehler ist. Vielleicht könnt ihr mich bei der Suche unterstützen?
Vielen Dank
Adhiambo
ich möchte aus dieser XML-Datei den Discount-Wert des entsprechenden condtions-Tags auslesen.
Conditions.xml
Code:
<?xml version="1.0" encoding="UTF-8"?>
<conditions>
<condition name="Ohne Skonto">
<discount>0.00</discount>
</condition>
<condition name="5 Tage">
<discount>2</discount>
</condition>
</conditions>
Dazu habe ich folgende Java-Klasse "ReadConditions.java"
Java:
package tld.software.calculator;
public class ReadConditions {
private XMLConditonsCreator element;
public static void main(String[] args) {
XMLConditonsCreator element = new XMLConditonsCreator();
System.out.println(element.readXml("Ohne Skonto"));
}
}
Constants.java:
Java:
package tld.software.calculator;
public class Constants {
public final static String ATTRIBUTE_NAME = "name";
public final static String ELEMENT_CONDITIONS = "conditions";
public final static String ELEMENT_CONDITION = "condition";;
public final static String ELEMENT_DISCOUNT = "discount";
}
Java:
package tld.software.calculator;
import static tld.software.calculator.Constants.ATTRIBUTE_NAME;
import static tld.software.calculator.Constants.ELEMENT_CONDITIONS;
import static tld.software.calculator.Constants.ELEMENT_CONDITION;
import static tld.software.calculator.Constants.ELEMENT_DISCOUNT;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import org.jdom2.*;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;
import org.jdom2.xpath.XPathFactory;
import org.jdom2.xpath.XPathExpression;
import org.xml.sax.SAXException;
import javax.xml.xpath.XPathExpressionException;
public class XMLConditonsCreator {
private static String conditionFile = System.getProperties().getProperty("user.home")
+File.separator+PlatformDependence.offerValue("directory")+File.separator + "Conditions.xml";
private static File xmlFile = new File(conditionFile);
private Namespace ns = Namespace.getNamespace("http://local.software.tld/calculator");
private SAXBuilder builder = new SAXBuilder();
private Document jdomDoc;
public XMLConditonsCreator() {
super();
}
public List<String> readXml(String conditionName) {
List<String> results = new ArrayList<String>();
List<Element> elements = getConditionsElements(conditionName);
if (elements != null) {
results.add(conditionName);
for (Element element : elements) {
results.add(element.getValue());
}
}
return results;
}
private static List<Element> getRootElements() {
SAXBuilder builder = null;
Document jdomDoc = null;
try {
builder = new SAXBuilder();
jdomDoc = builder.build(xmlFile);
} catch (JDOMException | IOException ex) {
System.err.println(ex);
}
if (builder != null && jdomDoc != null) {
return jdomDoc.getRootElement().getChildren();
} else {
return null;
}
}
private static List<Element> getConditionsElements(String conditionName) {
List<Element> serviceList = getRootElements();
if (serviceList != null) {
for (Element element : serviceList) {
try {
if (element.getAttribute("name").equals(conditionName)) {
return element.getChildren();
}
} catch ( NullPointerException ex ) {
ex.printStackTrace();
System.out.println("getConditionsElements wirft diese Exception");
}
}
}
return null;
}
public List<String> ConditionList() {
List<String> ConditionList = new ArrayList<>();
try {
if (new File(System.getProperties().getProperty("user.home")+
File.separator+PlatformDependence.offerValue("directory")+
File.separator+PlatformDependence.offerValue("setting")).exists()) {
jdomDoc = builder.build(xmlFile);
}
XPathFactory xFactory = XPathFactory.instance();
XPathExpression xpath = xFactory.compile("conditions/condition/@name");
for (Object opject : xpath.evaluate(jdomDoc)) {
ConditionList.add(((Attribute)opject).getValue());
}
} catch (JDOMException | IOException ex) {
ex.printStackTrace();
}
return ConditionList;
}
}
Mein Problem ist, dass diese Konstruktion ein leeres Array zurückliefert, obwohl ein Array mit einem Wert zurückkommen müsste.
Wenn ich die ReadConditions.java mit dem Debugger starte und den Breakpoint bei System.out.println setze, sehe ich dass alles fehlerfrei durchläuft. Nur der Rückgabewert in readXml(String conditionName) results liefert "size=0" und in der For-Schleife ist "elements: null". So wird der Hauptklasse ein leeres Array als Ergebnis übergeben.
Ich sehe im Moment nicht, wo mein Ansatzfehler ist. Vielleicht könnt ihr mich bei der Suche unterstützen?
Vielen Dank
Adhiambo