1
votes

I have a XML file containing

    <jaxbBean file="A.groovy"/>
    <jaxbBean file="B.groovy"/>

Now I'd like to get a List<String> out of that, containing "A.groovy", "B.groovy".

I've tried (and expected to work):

@XmlPath("jaxbBean/@file")
List<String> jaxbBeansClasses;

But that didn't match anything (contained null).

Is MOXy capable of doing this so simply? Or do I have to introduce an extra class?

(I don't want to change the XML syntax.)

1

1 Answers

0
votes

Your mappings look correct, below is a full example. Since you are annotating the field be sure you have @XmlAccessorType(XmlAccessType.FIELD) on your class (see: http://blog.bdoughan.com/2011/06/using-jaxbs-xmlaccessortype-to.html).

Domain Model (Root)

import java.util.List;
import javax.xml.bind.annotation.*;
import org.eclipse.persistence.oxm.annotations.XmlPath;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Root {

    @XmlPath("jaxbBean/@file")
    List<String> jaxbBeansClasses;

}

jaxb.properties

To specify MOXy as your JAXB (JSR-222) provider you need to include a file called jaxb.properties in the same package as your domain model with the following entry (see: http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html):

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory

Demo

import java.io.File;
import javax.xml.bind.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Root.class);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        File xml = new File("src/forum17104179/input.xml");
        Root root = (Root) unmarshaller.unmarshal(xml);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(root, System.out);
    }

}

input.xml/Output

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <jaxbBean file="A.groovy"/>
    <jaxbBean file="B.groovy"/>
</root>