JAXB add element dynamically

abhishek221192

Is it possible to create XML Document by JAXB in such way. As copy is between mkdir and it will be dynamic

<project>
    <create>
        <mkdir dest="src/java"/>
        <copy src="test/src/java" dest="src/java" />
        <mkdir dest="{projectPath}/web/META-INF"/>
    </create>
</project>

My java Code :-

@XmlRootElement(name = "project")
public class Project {    
    private Create create;
    private Tree tree;    
    public Create getCreate() {
        return create;
    }    
    public void setCreate(Create create) {
        this.create = create;
    }    
    public Tree getTree() {
        return tree;
    }    
    public void setTree(Tree tree) {
        this.tree = tree;
    }    
}

@XmlRootElement
public class Create {

    private List<Mkdir> mkdir;    
    private Buildfile buildfile;  
    private Createfile createfile; 
    private Execute execute;
    private Copy copy;

   .....
}

I am able to achieve same by JDOM but i want to do it by Java Object.

Or i should use any other Java Library for this.

laune

You should use this annotation on class Create:

@XmlAccessorType(XmlAccessType.FIELD)
public class Create { ... }

Assuming there are no glitches in the classes you haven't shown (Tree, Buildfile, Mkdir,...) you can use the following code (where String serves as a replacement for all classes unknown to me) to marshal an XML file:

    void marshal() throws Exception {
        Project root = new Project();
        Create create = new Create();
        root.setCreate( create );
        create.getMkdir().add( "make dir1" );
        create.getMkdir().add( "make dir2" );
        create.setBuildfile( "buildfile1" );
        create.setCreatefile( "createfile1" );
        JAXBContext jc = JAXBContext.newInstance( Project.class );
        Marshaller m = jc.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        m.marshal( root, System.out );
    }

Output:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<project>
  <create>
    <mkdir>make dir1</mkdir>
    <mkdir>make dir2</mkdir>
    <buildfile>buildfile1</buildfile>
    <createfile>createfile1</createfile>
  </create>
</project>

I suppose the mkdirs can all be up front. If it has to be mixed, it's a little more complicated.

Edit You need a list capable of holding elements of mixed types, best subtypes of a common type.

@XmlAccessorType(XmlAccessType.FIELD)
public class Create {

    @XmlElements({
        @XmlElement(name = "mkdir", type = MkdirCommand.class),
        @XmlElement(name = "copy", type = CopyCommand.class)
    })
    private List cmds;
    public List getCmds(){
    if( cmds == null ) cmds = new ArrayList();
    return cmds;
    }
}

class Command {
}

class MkdirCommand extends Command {
    @XmlAttribute
    public String dest;
    public MkdirCommand( String d ){ dest = d; }
}

class CopyCommand extends Command {
    @XmlAttribute
    public String src;
    @XmlAttribute
    public String dest;
    public CopyCommand( String s, String d ){ src = s; dest = d; }
}

Output:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<project>
  <create>
    <mkdir dest="foo/dir1"/>
    <copy src="source" dest="target"/>
    <mkdir dest="bar/dir2"/>
  </create>
</project>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Dynamically add element to layout

From Dev

Dynamically add/remove element

From Dev

JAXB add additional static root element

From Dev

Add class to dynamically added element

From Dev

Dynamically add specific attr to element

From Dev

Dynamically add element to XML file

From Dev

Add element on top of image dynamically

From Dev

Dynamically add element in JS to XUL

From Dev

Dynamically add element using angularJS

From Dev

Add element on top of image dynamically

From Dev

dynamically add attribute to child element

From Dev

JAXB Marshalling supply name space for root element dynamically

From Dev

Dynamically add value to XMLRootElement or XMLElement annotation using JAXB

From Dev

How to add an additional XML element for polymorphic types with JAXB

From Dev

How to add CSS class dynamically to html element

From Dev

How to add dynamically extended element with Polymer?

From Dev

Create div element and dynamically add child elements on it

From Dev

Add data to dynamically created element and bind event

From Dev

check if span element exists and then add div dynamically

From Dev

How to dynamically add a directive to an input element in AngularJS

From Dev

Add multiple html element dynamically using jquery

From Dev

Dynamically add angular attributes to an element from a directive

From Dev

jQuery: Dynamically add class that belongs to another element

From Dev

Add list elements dynamically to ul element

From Dev

How to add canvas to div element dynamically?

From Dev

jQuery - Add click event to a dynamically created element

From Dev

Dojo - add event listener to dynamically created element

From Dev

Dynamically add new element to specific list

From Dev

dynamically add new md autocomplete element

Related Related

  1. 1

    Dynamically add element to layout

  2. 2

    Dynamically add/remove element

  3. 3

    JAXB add additional static root element

  4. 4

    Add class to dynamically added element

  5. 5

    Dynamically add specific attr to element

  6. 6

    Dynamically add element to XML file

  7. 7

    Add element on top of image dynamically

  8. 8

    Dynamically add element in JS to XUL

  9. 9

    Dynamically add element using angularJS

  10. 10

    Add element on top of image dynamically

  11. 11

    dynamically add attribute to child element

  12. 12

    JAXB Marshalling supply name space for root element dynamically

  13. 13

    Dynamically add value to XMLRootElement or XMLElement annotation using JAXB

  14. 14

    How to add an additional XML element for polymorphic types with JAXB

  15. 15

    How to add CSS class dynamically to html element

  16. 16

    How to add dynamically extended element with Polymer?

  17. 17

    Create div element and dynamically add child elements on it

  18. 18

    Add data to dynamically created element and bind event

  19. 19

    check if span element exists and then add div dynamically

  20. 20

    How to dynamically add a directive to an input element in AngularJS

  21. 21

    Add multiple html element dynamically using jquery

  22. 22

    Dynamically add angular attributes to an element from a directive

  23. 23

    jQuery: Dynamically add class that belongs to another element

  24. 24

    Add list elements dynamically to ul element

  25. 25

    How to add canvas to div element dynamically?

  26. 26

    jQuery - Add click event to a dynamically created element

  27. 27

    Dojo - add event listener to dynamically created element

  28. 28

    Dynamically add new element to specific list

  29. 29

    dynamically add new md autocomplete element

HotTag

Archive