Hallo,
ich möchte eine entsprechende XML einlesen, die folgende Struktur hat:
die Tags sind entsprechend auch wieder geschlossen...
Wie kann ich das genau machen?
Muss ich mir entsprechende Java Klassen für jeden Knoten erstellen?
Und lese dann so ein:
danke für jede Hilfe
ich möchte eine entsprechende XML einlesen, die folgende Struktur hat:
Java:
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<NML VERSION="14">
<HEAD COMPANY="www.test.com" PROGRAM="Test"></HEAD>
<MUSICFOLDERS></MUSICFOLDERS>
<COLLECTION ENTRIES="11888">
<ENTRY MODIFIED_DATE="2010/12/3" MODIFIED_TIME="58906" TITLE="The Way I Do" ARTIST="2-4 Grooves">
<LOCATION VOLUME="C:" VOLUMEID="084e4720"></LOCATION>
<ALBUM TITLE="Kontor House Of House 2"></ALBUM>
die Tags sind entsprechend auch wieder geschlossen...
Wie kann ich das genau machen?
Muss ich mir entsprechende Java Klassen für jeden Knoten erstellen?
Code:
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Nml {
@XmlAttribute
private String version;
private Head head;
// getter, setter and toString...
}
@XmlAccessorType(XmlAccessType.FIELD)
public class Head {
@XmlAttribute
private String company;
@XmlAttribute
private String program;
private Collection collection;
// getter, setter and toString...
}
@XmlAccessorType(XmlAccessType.FIELD)
public class Collection {
@XmlAttribute
private int entries;
@XmlAttribute
private String program;
private List<Entry> entry;
}
@XmlAccessorType(XmlAccessType.FIELD)
public class Entry {
@XmlAttribute
private String title;
private List<Entry> entry;
}
Und lese dann so ein:
Java:
public class Test {
public static void main(String[] args) {
JAXBContext jaxbContext = null;
try {
jaxbContext = org.eclipse.persistence.jaxb.JAXBContextFactory
.createContext(new Class[]{Nml.class}, null);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
// output pretty printed
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
// output to a xml file
jaxbMarshaller.marshal(o, new File("C:\\test\\myfile.xml"));
} catch (JAXBException e) {
e.printStackTrace();
}
}
}
danke für jede Hilfe