简体   繁体   English

我想使用Java将子元素插入xml树中的子元素

[英]I want to insert subelement to a subelement in xml tree using Java

@XmlRootElement
public class Dekkey {
    String keyVal;
    String kek1;

    public String getKek1() {
        return kek1;
    }

    @XmlElement
    public void setKek1(String kek1) {
        this.kek1 = kek1;
    }

    public String getKeyval() {
        return keyVal;
    }

    @XmlAttribute
    public void setKeyval(String inpKey) {
        this.keyVal = inpKey;
    }
}

This is my code snippet, where I want to insert a sub-element called userkey to the sub-element kek1. 这是我的代码段,我想在其中向子元素kek1插入一个名为userkey的子元素。 How can I do that? 我怎样才能做到这一点?

How to insert attribute value for those sub-elements? 如何为这些子元素插入属性值? I have another class called MarshDemo in which an object of Dekkey is created and then setkeyVal() function is called by passing value to the function. 我有另一个名为MarshDemo类,其中创建了一个Dekkey对象,然后通过将值传递给该函数来调用setkeyVal()函数。

The output looks like this: 输出看起来像这样:

<Dekkey keyVal="xer">
    <kek1 keyVal="biv">
        <userkey keyVal="wed">
        </userkey>
    </kek1>
</Dekkey>

I have omitted the getters and setters for brevity, this is what your should look like. 为了简洁起见,我省略了getter和setter方法,这就是您的外观。

@XmlRootElement
public class Dekkey {
    @XmlAttribute
    String keyVal;

    Kek1 kek1;
}

@XmlElement(name="kek1")
public class Kek1 {
    @XmlAttribute
    String keyVal;

    UserKey userkey;
}


@XmlElement(name="userkey")
public class UserKey {
    @XmlAttribute
    String keyVal;
}

Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB (JSR-222) expert group. 注意:我是EclipseLink JAXB(MOXy)的负责人,并且是JAXB(JSR-222)专家组的成员。

Dekkey 德基

Below is how you could map your use case using MOXy's @XmlPath extension (see: http://blog.bdoughan.com/2010/07/xpath-based-mapping.html ). 以下是使用MOXy的@XmlPath扩展来映射用例的@XmlPath (请参阅: http : @XmlPath )。

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

@XmlRootElement(name="Dekkey")
@XmlAccessorType(XmlAccessType.FIELD)
public class Dekkey {

    @XmlAttribute
    String keyVal;

    @XmlPath("kek1/@keyVal")
    String kek1;

    @XmlPath("kek1/userkey/@keyVal")
    String userKey;

}

jaxb.properties jaxb.properties

To specify MOXy as your JAXB (JSR-222) 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 ). 要将MOXy指定为JAXB(JSR-222)提供程序,您需要在与域模型相同的包中包含一个名为jaxb.properties的文件,并带有以下条目(请参阅: http : //blog.bdoughan.com/2011/05) /specifying-eclipselink-moxy-as-your.html )。

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

Demo 演示版

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

public class Demo {

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

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        File xml = new File("src/forum16248263/input.xml");
        Dekkey dekkey = (Dekkey) unmarshaller.unmarshal(xml);

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

}

input.xml/Output input.xml /输出

<?xml version="1.0" encoding="UTF-8"?>
<Dekkey keyVal="xer">
   <kek1 keyVal="biv">
      <userkey keyVal="wed"/>
   </kek1>
</Dekkey>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM