0

I am trying to create an xml file for Solr, but I run into a problem. I use the @XmlElement annotation to tell the marhaller which are the xml elements of my class like so:

@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");
}

and so on.

Then I use the following to create the xml file:

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);

It works, but I want my xml file to have the following format:

<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 is currently like the following:

<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>

What I want to do is to add the name attribute in each field element. Is this possible by using the @Xml... annotations?

  • Look at my answer there https://stackoverflow.com/questions/47877912/formatting-xml-with-jaxb/47878150#47878150 - looks like it is the same assesment. – Vadim Dec 19 '17 at 14:39
  • Possible duplicate of [Formatting XML with JAXB](https://stackoverflow.com/questions/47877912/formatting-xml-with-jaxb) – dur Dec 19 '17 at 14:40
  • XML attribute values are handled in a different way, see here for example: https://stackoverflow.com/questions/5514752/xml-element-with-attribute-and-content-using-jaxb – Alex Savitsky Dec 19 '17 at 14:41

1 Answers1

-1

if you would like to use attr and value you need complex element and @XmlValue annotation:

@XmlElement(name = "doc")
@XmlAccessorType(XmlAccessType.FIELD)
private List<Node> list;

@XmlElement(name = "field")
@XmlAccessorType(XmlAccessType.FIELD)
private Node node;

@XmlAccessorType(XmlAccessType.FIELD)
class Node {
@XmlAttribute(name= "name")
private String attr;

@XmlValue
private Srting value;
}
Alex A.
  • 39
  • 1
  • 4