简体   繁体   中英

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

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.

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

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

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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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