如何使用JAXB在java中的另一个元素中创建元素?

如何使用JAXB在java中的另一个元素中创建元素?,java,xml,jaxb,parent-child,Java,Xml,Jaxb,Parent Child,我有一个使用JAXB创建XML文件的java代码,我知道如何创建根元素和元素,但我想从如下元素创建子元素: <root element> <element> <child element> <group of elements and attributes> </child element> </ele

我有一个使用JAXB创建XML文件的java代码,我知道如何创建根元素和元素,但我想从如下元素创建子元素:

    <root element>
        <element>
               <child element>
               <group of elements and attributes>
               </child element>
        </element>
    </root element>

我现在只知道如何这样创造:

    <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> 

local.yahoo.com
csz
开罗,+CA
/rss/餐厅
那么,如果我想在主机名中设置如下参数,我该怎么办:

<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>

csz
LosAngelos,+CA
/rss/餐厅
最后是我使用的java代码:

我放置注释的类:

       @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;
    }
      }
@XmlRootElement(name=“Source”)
公共类XmlConf{
@xmlement(name=“Source”)
私有URL;
私有列表路径=新的ArrayList();
私有字符串urlp;
私有映射参数;
私有字符串主机;
公共URL getUrl(){
返回url;
}
@xmldattribute(name=“URL”)
公共无效设置URL(URL){
this.url=url;
}
@XmlElement
公共列表getPath(){
返回路径;
}
公共void setPath(字符串路径){
this.path.add(path);
}
@XmlElement
公共void setUrlPath(字符串urlp){
this.urlp=urlp;
}
公共字符串getUrlPath(){
返回urlp;
}
@xmlement(name=“param”)
公共void setParameters(映射参数){
此参数=参数;
}
公共映射getParameters(){
返回参数;
}
public void setHostName(字符串主机){
this.host=host;
}
公共字符串getHostName(){
返回主机;
}
}
我在中使用JAXB的类:

    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);

    }
      }
公共类冲突列表{
私有静态最终字符串fileName=“Source.xml”;
列表列表;
私有客体;
公共冲突列表(对象){
this.object=对象;
}
public void addToList()引发IOException、JAXBEException{
文件文件=新文件(对象+文件名);
JAXBContext JAXBContext=JAXBContext.newInstance(XmlConf.class);
Marshaller jaxbMarshaller=jaxbContext.createMarshaller();
setProperty(Marshaller.JAXB_格式化的_输出,true);
jaxbMarshaller.marshall(对象,文件);
jaxbMarshaller.marshall(object,System.out);
}
}

您可以继续创建第二个类“条目”,将属性“key”和“value”作为字段,然后在“XmlConf”中这样更改字段

私有列表参数;

干杯//Lutz

您希望生成的XML具有所谓的混合内容。混合内容是指元素(
hostName
)同时包含文本(
local.yahoo.com
)和元素(
parameters
)内容

<XmlSource URL="http://local.yahoo.com/rss/restaurants?csz=Cairo,+CA">
    <hostName>local.yahoo.com
        <parameters>
            ...
        </parameters>
    </hostName>
</XmlSource>

更新


是的,你说得对,我的错,我在找那张表格,不是我想要的那张 写的。我怎样才能做到

注意:我是专家组的负责人和成员

对于此用例,您可以利用MOXy的
@XmlPath
扩展:

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;

}
import java.net.URL;
导入java.util.*;
导入javax.xml.bind.annotation.*;
导入org.eclipse.persistence.oxm.annotations.XmlPath;
@XmlRootElement(name=“XmlSource”)
@XmlType(propOrder={“参数”、“urlp”、“路径”})
@XmlAccessorType(XmlAccessType.FIELD)
公共类XmlConf{
@xmldattribute(name=“URL”)
私有URL;
私有列表路径=新的ArrayList();
@xmlement(name=“urlPath”)
私有字符串urlp;
@XmlPath(“主机名/参数”)
私有映射参数;
@XmlPath(“主机名/@name”)
私有字符串主机;
}
了解更多信息


但是如何为列表中的每个元素指定名称,我只能更改值而不能更改名称是的,你说得对,我的错,我在找的表单不是我写的表单。我该怎么做?@muhammedreaat-我已经用一种方法更新了我的答案来映射这个用例。
<XmlSource URL="http://local.yahoo.com/rss/restaurants?csz=Cairo,+CA">
    <host name="local.yahoo.com">
        <parameters>
            ...
        </parameters>
    </host>
</XmlSource>
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;

}