Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XML mapping attribute of nested element

I'm using XStream and I have an XML sample:

<person>
    <firstname>Joe</firstname>
    <lastname>Walnes</lastname>
    <phone value="1234-456" />
    <fax value="9999-999" />
</person>

and I whant to map it to the class

public class Person {

    private String firstname;
    private String lastname;
    private String phone;
    private String fax;

}

So the idea is to map attribute of nested element to the current object. I tried to find any ready-to-use converter with no success. I believe that's possible by implementing new converter but may be someone already did this. Or there's a solution I haven't found.

Updated:

The idea I'm trying to implement is omitting unnecessary entities of being created and mapped. I don't need Phone and Fax entities at all, I need only their attributes in my model. The XML schema I'm trying to parse is thirdparty for me and I can't change it.

like image 851
Viktor Stolbin Avatar asked Sep 14 '12 13:09

Viktor Stolbin


2 Answers

I don't know of a ready-to-use converter that will do it, but it's pretty trivial to write one

import com.thoughtworks.xstream.converters.*;
import com.thoughtworks.xstream.io.*;

public class ValueAttributeConverter implements Converter {
  public boolean canConvert(Class cls) {
    return (cls == String.class);
  }

  public void marshal(Object source, HierarchicalStreamWriter w, MarshallingContext ctx) {
    w.addAttribute("value", (String)source);
  }

  public Object unmarshal(HierarchicalStreamReader r, UnmarshallingContext ctx) {
    return r.getAttribute("value");
  }
}

You can attach the converter to the relevant fields using annotations

import com.thoughtworks.xstream.annotations.*;

@XStreamAlias("person")
public class Person {

    private String firstname;
    private String lastname;

    @XStreamConverter(ValueAttributeConverter.class)
    private String phone;

    @XStreamConverter(ValueAttributeConverter.class)
    private String fax;

    // add appropriate constructor(s)

    /** For testing purposes - not required by XStream itself */
    public String toString() {
      return "fn: " + firstname + ", ln: " + lastname +
             ", p: " + phone + ", f: " + fax;
    }
}

To make this work, all you need to do is instruct XStream to read the annotations:

XStream xs = new XStream();
xs.processAnnotations(Person.class);
Person p = (Person)xs.fromXML(
  "<person>\n" +
  "  <firstname>Joe</firstname>\n" +
  "  <lastname>Walnes</lastname>\n" +
  "  <phone value='1234-456' />\n" +
  "  <fax value='9999-999' />\n" +
  "</person>");
System.out.println(p);
// prints fn: Joe, ln: Walnes, p: 1234-456, f: 9999-999
like image 200
Ian Roberts Avatar answered Oct 28 '22 17:10

Ian Roberts


Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB (JSR-222) expert group.

If you are open to using a library other than XStream, below is how you could leverage the @XmlPath extension in EclipseLink JAXB (MOXy).

Person

The @XmlPath annotation allows you to map your field/property to a location in the XML document by an XPath (see: http://blog.bdoughan.com/2010/07/xpath-based-mapping.html).

package forum12425401;

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

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

    private String firstname;
    private String lastname;

    @XmlPath("phone/@value")
    private String phone;

    @XmlPath("fax/@value")
    private String fax;

}

jaxb.properties

To specify MOXy as your JAXB 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

The code below will convert the XML into your domain model, and then write the domain model back to XML.

package forum12425401;

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

public class Demo {

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

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        File xml = new File("src/forum12425401/input.xml");
        Person person = (Person) unmarshaller.unmarshal(xml);

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

}

input.xml/Output

<?xml version="1.0" encoding="UTF-8"?>
<person>
   <firstname>Joe</firstname>
   <lastname>Walnes</lastname>
   <phone value="1234-456"/>
   <fax value="9999-999"/>
</person>
like image 23
bdoughan Avatar answered Oct 28 '22 16:10

bdoughan