Ich versuche, eine XML-Datei für Solr zu erstellen, aber ich stoße auf ein Problem. Ich verwende die @XmlElement Annotation die marhaller zu sagen, welche die XML-Elemente meiner Klasse sind etwa so:Hinzufügen von xmlAttribute auf xmlElement in Java
@XmlElement(name = "field")
public String getAuthors() {
return authors;
}
public void setAuthors(List<String> authors) {
setValue(authors, "authors");
}
@XmlElement(name = "field")
public String getDate() {
return date;
}
public void setDate(List<String> date) {
setValue(date, "date");
}
@XmlElement(name = "field", required = false)
public String getContent() {
return content;
}
public void setContent(List<String> content) {
setValue(content, "content");
}
und so weiter.
Dann benutze ich die im Anschluss an die XML-Datei zu erstellen:
XMLCreator collection = new XMLCreator(docs);
File fileOut = new File("docs.xml");
BufferedWriter writer = new BufferedWriter(new FileWriter(fileOut));
JAXBContext jaxbContext = JAXBContext.newInstance(XMLCreator.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.marshal(collection, writer);
Es funktioniert, aber ich möchte, dass meine XML-Datei das folgende Format haben:
<add>
<doc>
<field name="authors">Patrick Eagar</field>
<field name="subject">Sports</field>
<field name="dd">796.35</field>
<field name="numpages">128</field>
<field name="desc"></field>
<field name="price">12.40</field>
<field name="title">Summer of the all-rounder: Test and championship cricket in England 1982</field>
<field name="isbn">0002166313</field>
<field name="yearpub">1982</field>
<field name="publisher">Collins</field>
</doc>
<doc>
...
</doc>
</add>
-Mine ist derzeit wie folgt aus :
<add>
<doc id="2">
<field></field>
<field>Sugai, I.</field>
<field></field>
<field>CACM December, 1958 </field>
<field>CA581202 JB March 22, 1978 8:29 PM </field>
<field></field>
<field>2 5 2
2 5 2
2 5 2
</field>
<field>Extraction of Roots by Repeated Subtractions for Digital
Computers </field>
</doc>
</add>
Was ich tun möchte, ist das Attribut name in jedem Feldelement hinzufügen. Ist das möglich mit den @ Xml ... Annotationen?
Blick auf meine Antwort gibt https://stackoverflow.com/questions/47877912/formatting-xml-with -jaxb/47878150 # 47878150 - sieht so aus, als ob es das selbe ist. – Vadim
Mögliches Duplikat von [Formatieren von XML mit JAXB] (https://stackoverflow.com/questions/47877912/formatting-xml-with-jaxb) – dur
XML-Attributwerte werden auf andere Weise behandelt, siehe hier zum Beispiel: https://stackoverflow.com/questions/5514752/xml-element-with-attribute-and-content-using-jaxb –