Reputation: 9103
I have a java code i'm using to create XML file using JAXB, i know how to create root elements and elements but i want to create a child elements from an element like that:
<root element>
<element>
<child element>
<group of elements and attributes>
</child element>
</element>
</root element>
I'm now only know how to create like this:
<XmlSource URL="http://local.yahoo.com/rss/restaurants?csz=Cairo,+CA">
<hostName>local.yahoo.com</hostName>
<parameters>
<entry>
<key>csz</key>
<value>Cairo,+CA</value>
</entry>
</parameters>
<urlPath>/rss/restaurants</urlPath>
</XmlSource>
So, what will i do if i want to put the parameters inside the hostName to be like that :
<XmlSource URL="http://local.yahoo.com/rss/restaurants?csz=Cairo,+CA">
<hostName name="local.yahoo.com">
<parameters>
<entry>
<key>csz</key>
<value>LosAngelos,+CA</value>
</entry>
</parameters>
</hostName>
<urlPath>/rss/restaurants</urlPath>
</XmlSource>
And finally that's the java code i use :
The class i put annotations at :
@XmlRootElement(name= "Source")
public class XmlConf {
@XmlElement(name= "Source")
private URL url;
private List<String> path = new ArrayList<String>();
private String urlp;
private Map<String, String> parameters;
private String host;
public URL getUrl() {
return url;
}
@XmlAttribute(name = "URL")
public void setUrl(URL url) {
this.url = url;
}
@XmlElement
public List<String> getPath() {
return path;
}
public void setPath(String path) {
this.path.add(path);
}
@XmlElement
public void setUrlPath(String urlp){
this.urlp = urlp;
}
public String getUrlPath(){
return urlp;
}
@XmlElement(name = "param")
public void setParameters(Map<String, String> parameters){
this.parameters = parameters;
}
public Map<String, String> getParameters(){
return parameters;
}
public void setHostName(String host){
this.host = host;
}
public String getHostName(){
return host;
}
}
the class i use JAXB in :
public class ConfList {
private static final String fileName = "Source.xml";
List<String> xmlConfList;
private Object object;
public ConfList(Object object){
this.object = object;
}
public void addToList() throws IOException, JAXBException {
File file = new File(object+fileName);
JAXBContext jaxbContext = JAXBContext.newInstance(XmlConf.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.marshal(object, file);
jaxbMarshaller.marshal(object, System.out);
}
}
Upvotes: 1
Views: 6611
Reputation: 149017
The XML you are looking to produce has what is called mixed content. Mixed content is when a element (hostName
) has both text (local.yahoo.com
) and element (parameters
) content.
<XmlSource URL="http://local.yahoo.com/rss/restaurants?csz=Cairo,+CA">
<hostName>local.yahoo.com
<parameters>
...
</parameters>
</hostName>
</XmlSource>
This can be mapped with JAXB with the @XmlMixed
annotation where the text and element content go into one list, but this probably isn't what you want. How about something like this instead?
<XmlSource URL="http://local.yahoo.com/rss/restaurants?csz=Cairo,+CA">
<host name="local.yahoo.com">
<parameters>
...
</parameters>
</host>
</XmlSource>
UPDATE
yes, y're right, my bad, i was looking for that form not the one i wrote. how can i make it?
Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB (JSR-222) expert group.
You could leverage MOXy's @XmlPath
extension for this use case:
import java.net.URL;
import java.util.*;
import javax.xml.bind.annotation.*;
import org.eclipse.persistence.oxm.annotations.XmlPath;
@XmlRootElement(name = "XmlSource")
@XmlType(propOrder={"parameters", "urlp", "path"})
@XmlAccessorType(XmlAccessType.FIELD)
public class XmlConf {
@XmlAttribute(name="URL")
private URL url;
private List<String> path = new ArrayList<String>();
@XmlElement(name="urlPath")
private String urlp;
@XmlPath("hostName/parameters")
private Map<String, String> parameters;
@XmlPath("hostName/@name")
private String host;
}
For More Information
Upvotes: 3
Reputation: 340
you could go ahead and create a second class "Entry" that would take your attributes "key" and "value" as fields and then change your field in "XmlConf" like so
private List<Entry> parameters;
Cheers //Lutz
Upvotes: 0