Untitled
unknown
plain_text
2 years ago
4.8 kB
6
Indexable
import javax.xml.bind.JAXBContext;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import java.io.File;
import java.util.List;
import java.util.stream.Collectors;
@XmlRootElement(name = "dbtplenarprotokoll")
public class PlenaryProtocol {
private Vorspann vorspann;
@XmlElement(name = "vorspann")
public Vorspann getVorspann() {
return vorspann;
}
public void setVorspann(Vorspann vorspann) {
this.vorspann = vorspann;
}
public static void main(String[] args) {
String xmlFilePath = "path/to/your/file.xml";
try {
// XML Parsing using DocumentBuilderFactory
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
docBuilderFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document document = docBuilder.parse(new File(xmlFilePath));
// JAXB Unmarshalling
JAXBContext jaxbContext = JAXBContext.newInstance(PlenaryProtocol.class);
PlenaryProtocol protocol = (PlenaryProtocol) jaxbContext.createUnmarshaller().unmarshal(document);
// Accessing the first names of all speakers
if (protocol.getVorspann() != null && protocol.getVorspann().getInhaltsverzeichnis() != null) {
List<String> allFirstNames = protocol.getVorspann().getInhaltsverzeichnis().getAllFirstNames();
// Outputting the first names
System.out.println("All first names of the speakers:");
for (String firstName : allFirstNames) {
System.out.println(firstName);
}
} else {
System.out.println("The XML structure is not as expected.");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
class Vorspann {
private Inhaltsverzeichnis inhaltsverzeichnis;
@XmlElement(name = "inhaltsverzeichnis")
public Inhaltsverzeichnis getInhaltsverzeichnis() {
return inhaltsverzeichnis;
}
public void setInhaltsverzeichnis(Inhaltsverzeichnis inhaltsverzeichnis) {
this.inhaltsverzeichnis = inhaltsverzeichnis;
}
}
class Inhaltsverzeichnis {
private List<IvzEintrag> ivzEintragList;
@XmlElement(name = "ivz-eintrag")
public List<IvzEintrag> getIvzEintragList() {
return ivzEintragList;
}
public void setIvzEintragList(List<IvzEintrag> ivzEintragList) {
this.ivzEintragList = ivzEintragList;
}
// Method to extract all first names of the speakers
public List<String> getAllFirstNames() {
if (ivzEintragList != null) {
return ivzEintragList.stream()
.filter(ivzEintrag -> ivzEintrag.getRedner() != null)
.map(IvzEintrag::getRedner)
.map(Redner::getVorname)
.collect(Collectors.toList());
} else {
return List.of(); // Return an empty list if ivzEintragList is null
}
}
}
class IvzEintrag {
private Redner redner;
private String ivzEintragInhalt;
@XmlElement(name = "redner")
public Redner getRedner() {
return redner;
}
public void setRedner(Redner redner) {
this.redner = redner;
}
@XmlElement(name = "ivz-eintrag-inhalt")
public String getIvzEintragInhalt() {
return ivzEintragInhalt;
}
public void setIvzEintragInhalt(String ivzEintragInhalt) {
this.ivzEintragInhalt = ivzEintragInhalt;
}
}
class Redner {
private String titel;
private String vorname;
private String nachname;
private String fraktion;
@XmlElement(name = "titel")
public String getTitel() {
return titel;
}
public void setTitel(String titel) {
this.titel = titel;
}
@XmlElement(name = "vorname")
public String getVorname() {
return vorname;
}
public void setVorname(String vorname) {
this.vorname = vorname;
}
@XmlElement(name = "nachname")
public String getNachname() {
return nachname;
}
public void setNachname(String nachname) {
this.nachname = nachname;
}
@XmlElement(name = "fraktion")
public String getFraktion() {
return fraktion;
}
public void setFraktion(String fraktion) {
this.fraktion = fraktion;
}
}
Editor is loading...
Leave a Comment