Question

I need converter this xml to Java Object, I was using a manual DOM w3c (DocumentBuilder).

But I would use the JAXB,

This is XML

<?xml version="1.0" encoding="UTF-8"?>
<user-agent>
    <style-class>{prefix} {prefix}-{0}</style-class>
    <template name="basic" default="true">/META-INF/page-basic.xhtml</template>
    <template name="frame">/META-INF/page-frame.xhtml</template>
    <support id="MSIE-OLD">
        <pattern>.*MSIE (4|5|6|7|8).*</pattern>
        <prefix>ie-old</prefix>
        <template name="no-support" default="true">/META-INF/no-support.xhtml</template>
        <style-class>no-support {prefix}</style-class>
    </support>
    <support id="MSIE">
        <pattern>.*MSIE (\d+).*</pattern>
        <prefix>ie</prefix>
    </support>
    <support id="Firefox">
        <pattern>.*Firefox/(\d+).*</pattern>
        <prefix>fx</prefix>
    </support>
    <support id="Android-Fx">
        <pattern>.*Firefox/(\d+).*Android/(\d+).*</pattern>
        <prefix>fx</prefix>
        <template name="basic" default="true">/META-INF/movil-basic.xhtml</template>
        <template name="frame">/META-INF/movil-frame.xhtml</template>
        <style-class>fx fx-{0} android-{1}</style-class>
    </support>
</user-agent>

This is my java class:

public class Support implements Serializable {
  private final String id;
  private final Pattern pattern;
  private final String prefix;
  private final Map<String, String> templates = new HashMap();
  private final String defaultTemplate;
  private final String styleClass;
}

Help me please, I dont know how to uses case for default attribute and default template, I try implement my SupportAdapter (extends XmlAdapter)

Was it helpful?

Solution

you have to create Jaxb pojos for it

1. UserAgent

import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = { "styleClass", "template", "support" })
@XmlRootElement(name = "user-agent")
public class UserAgent {

    @XmlElement(name = "style-class", required = true)
    protected String styleClass;
    @XmlElement(required = true)
    protected List<Template> template;
    @XmlElement(required = true)
    protected List<Support> support;

    public String getStyleClass() {
        return styleClass;
    }

    public void setStyleClass(String value) {
        this.styleClass = value;
    }

    public List<Template> getTemplate() {
        if (template == null) {
            template = new ArrayList<Template>();
        }
        return this.template;
    }

    public List<Support> getSupport() {
        if (support == null) {
            support = new ArrayList<Support>();
        }
        return this.support;
    }

}

2. Support

import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = { "pattern", "prefix", "template", "styleClass" })
@XmlRootElement(name = "support")
public class Support {

    @XmlElement(required = true)
    protected String pattern;
    @XmlElement(required = true)
    protected String prefix;
    protected List<Template> template;
    @XmlElement(name = "style-class")
    protected String styleClass;
    @XmlAttribute
    protected String id;

    public String getPattern() {
        return pattern;
    }

    public void setPattern(String value) {
        this.pattern = value;
    }

    public String getPrefix() {
        return prefix;
    }

    public void setPrefix(String value) {
        this.prefix = value;
    }

    public List<Template> getTemplate() {
        if (template == null) {
            template = new ArrayList<Template>();
        }
        return this.template;
    }

    public String getStyleClass() {
        return styleClass;
    }

    public void setStyleClass(String value) {
        this.styleClass = value;
    }

    public String getId() {
        return id;
    }

    public void setId(String value) {
        this.id = value;
    }
}

3. Template

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.XmlValue;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = { "content" })
@XmlRootElement(name = "template")
public class Template {

    @XmlValue
    protected String content;
    @XmlAttribute(name = "default")
    protected String _default;
    @XmlAttribute
    protected String name;

    public String getContent() {
        return content;
    }

    public void setContent(String value) {
        this.content = value;
    }

    public String getDefault() {
        return _default;
    }

    public void setDefault(String value) {
        this._default = value;
    }

    public String getName() {
        return name;
    }

    public void setName(String value) {
        this.name = value;
    }

}

4. finally Parse it

JAXBContext jaxbContext = JAXBContext.newInstance(UserAgent.class);
            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
// pase file, url , inputstream , string whatever you have.
                UserAgent userAgent = (UserAgent) jaxbUnmarshaller.unmarshal(file);
                //getting <template> data from last <support> tag
                List<Template> templates = userAgent.getSupport().get(3).getTemplate();
                for (Template template : templates) {
                    String wantToSeeTempalteData = String.format(
                            "Tempalt [name:%s , default:%s content:%s ]",
                            new Object[] { template.getName(),template.getDefault(), template.getContent() });
                    System.out.println(wantToSeeTempalteData);
                }

Result

Tempalt [name:basic , default:true content:/META-INF/movil-basic.xhtml ]
Tempalt [name:frame , default:null content:/META-INF/movil-frame.xhtml ]

OTHER TIPS

You can start with this example by creating two classes Support and MyTemplate
import java.io.Serializable;

import java.util.List;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;


@XmlAccessorType(XmlAccessType.FIELD)
public class Support implements Serializable {
    @XmlAttribute
    private String id;
    @XmlElement
    private String pattern;
    @XmlElement
    private String prefix;
    @XmlElement(name = "style-class")
    private String styleClass;
    @XmlElement(name = "template")
   private List<MyTemplate> templateList;


}

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlValue;

public class MyTemplate  implements Serializable{
    @XmlAttribute
    private String name;
    @XmlAttribute(name = "default")
    private String defaultValue;
    @XmlValue
    private String value;

    public MyTemplate() {
        super();
    }
}

Use frameworks like XmlBeans http://xmlbeans.apache.org/

This would do the data conversion from xml to Java and vice versa.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top