1
votes

I currently use moxy on a project and I'm facing a problem for which I can't find a solution.

I use only a xml mapping file to map a xml received from a client to my java classes.

My problem is that an element name is "E-Mail" just like this:

[email protected]

In the xml received it's a list so I first try to map like this:

xml-element java-attribute="eMail" type="java.lang.String" xml-path="E-Mail/text()" container-type="java.util.List"

This expression work with same type of object but without dash in element name.

But it didn't work in this case. (the xml-path expression doesn't work).

I tried a lot of different expressions but i didn't find any solution...

Please could you help me ? Someone has already dealed with this kind of problem ?

1

1 Answers

0
votes

I'm not sure I fully understand your question, but I believe you are experiencing a problem when there is a dash in the element name. Below is an example that may help.

bindings.xml

Below is an example of EclipseLink JAXB (MOXy)'s XML binding file. The xml-path for the mapping to the email property contains a dash E-Mail/text().

<?xml version="1.0"?>
<xml-bindings
    xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
    package-name="forum10435322">
    <java-types>
        <java-type name="Root">
            <xml-root-element/>
            <java-attributes>
                <xml-element java-attribute="email" xml-path="E-Mail/text()"/>
            </java-attributes>
        </java-type>
    </java-types>
</xml-bindings>

Root

Below is the domain object I'll use for this example. It does not contain any annotations.

package forum10435322;

import java.util.List;

public class Root {

    private List<String> email;

    public List<String> getEmail() {
        return email;
    }

    public void setEmail(List<String> email) {
        this.email = email;
    }

}

jaxb.properties

In order to specify MOXy as the JAXB provider you need to add a file called jaxb.properties in the same package as your domain classes with the following entry:

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

Demo

Below is some demo code demonstrating how to bootstrap a JAXBContext using MOXy's external metadata.

package forum10435322;

import java.io.File;
import java.util.*;
import javax.xml.bind.*;
import org.eclipse.persistence.jaxb.JAXBContextFactory;

public class Demo {

    public static void main(String[] args) throws Exception {
        Map<String, Object> properties = new HashMap<String, Object>(1);
        properties.put(JAXBContextFactory.ECLIPSELINK_OXM_XML_KEY, "forum10435322/bindings.xml");
        JAXBContext jc = JAXBContext.newInstance(new Class[] {Root.class}, properties);

        File xml = new File("src/forum10435322/input.xml");
        Unmarshaller unmarshaller = jc.createUnmarshaller(); 
        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

The following is the input and output for this example.

<?xml version="1.0" encoding="UTF-8"?>
<root>
   <E-Mail>[email protected]</E-Mail>
   <E-Mail>[email protected]</E-Mail>
</root>