문제

I have XML:

<SyncMXAUTHCI>
  <MXAUTHCISet>
    <CI>
      <CINAME>COMPUTER68</CINAME>
      <CIRELATION>INSTALLED</CIRELATION>
    </CI>
  </MXAUTHCISet>
</SyncMXAUTHCI>

I would like to have duplicate content of MXAUTHCISet. Result would be:

<SyncMXAUTHCI>
  <MXAUTHCISet>
    <CI>
      <CINAME>COMPUTER68</CINAME>
    </CI>
    <CI>
      <CINAME>COMPUTER68</CINAME>
      <CIRELATION>INSTALLED</CIRELATION>
    </CI>
  </MXAUTHCISet>
</SyncMXAUTHCI>

How to do it? I tried with .addContent, .setContnet methods but without success. How to aachieve this? Thank you

UPDATE: I take elements in this form:

Document erJdom = erData.getData();
Element root = erJdom.getRootElement();
Namespace erJdomNamespace = root.getNamespace();

Element incidentSet = root.getChild("MXAUTHCISet", erJdomNamespace);

Element incident=incidentSet.getChild("CI", erJdomNamespace);

That work ok. But when I try:

Element incident=incidentSet.getChild("CI", erJdomNamespace);
Element ci2=new Element("CI");
ci2.addContent(incident);

So you can see that I try to take element content and to put it in new element with the same content which will i add on MXAUTHSet ERROR I am getting: The Content already has an existing parent "MXAUTHCISet"

so it even does not come to the part where I want to add that new element:

incidentSet.addContent(ci2);
도움이 되었습니까?

해결책

You cannot add any JDOM content to any Element if that content is already attached to an Element.

The easiest thing for you to do is use the clone() method which creates an un-attached duplicate.

In your case:

incidentSet.addContent((Element)incidentSet.getChild("CI", erJdomNamespace).clone());

(If you were using JDOM 2.0.x the clone() method will return an Element... and the (Element) case would be unnecessary)

다른 팁

You have to add your repeating elements to a list, and then use addContent to add the list at the correct place in the structure.

    Document d = new Document();
    Element r = new Element("SyncMXAUTHCI");

    d.setRootElement(r);

    Element e = new Element("MXAUTHCISet");
    r.addContent(e);

    Element ae1 = new Element("CI");
    Element ae2 = new Element("CI");

    Element e2 = new Element("CINAME");
    e2.setText("COMPUTER68");
    ae1.setContent(e2);

    Element e3 = new Element("CINAME");
    e3.setText("COMPUTER68");
    ae2.setContent(e3);

    List l = new ArrayList();
    l.add(ae1);
    l.add(ae2);

    e.addContent(l);

    System.out.println(new XMLOutputter().outputString(d));
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top