0

I'm writing an XML serializer with JAXP. I'm receiving pseudo random data from a JAR and I'm building the DOM tree. I have to check if I already inserted the same Element into the tree; in order to perform this control I'm trying to use the method:

Element e = myDocument.getElementById(ao.getId());
if (e == null) {
    // element is not a duplicate
    access.appendChild(authorizationObject);
}else{
    // element already in the tree 
}

So, in every Element I create before adding them to the tree I set:

ao = a.getAuthorizationObject();
authorizationObject = myDocument.createElement("authorizationobject");
authorizationObject.setAttribute("id", ao.getId());
authorizationObject.setIdAttribute("id", true);

It can happen that in the object ao sometimes I get the same element twice (or more). The problem is that the program always enter inside the if instruction.

You can find all the program's code here and the DTD here for your reference.

What am I doing wrong? Thanks in advance for all your reply.

Manuel Durando
  • 1,493
  • 3
  • 19
  • 30

1 Answers1

0

You have forgotten to append the authorizationObject to the access Element. Your code should be as follows

    authorizationObject = myDocument.createElement("authorizationobject");
    authorizationObject.setAttribute("id", ao.getId());

    authorizationObject.setIdAttribute("id", true);     
    System.out.println("AO.ID = " + ao.getId());
    access.appendChild(authorizationObject); 
    // then only this Element will be   appended to the document
    if (myDocument.getElementById(ao.getId()) == null ) { 

I see that you have finally appended the authorization object to the document. but, it should be done prior to document.getElementById() method call

Hope this helps!

Keerthivasan
  • 12,760
  • 2
  • 32
  • 53
  • Sorry, I wasn't clear enough in the original answer, I edited it now. I don't want to add another child if already present into the tree. If you checked the linked file before it was already implemented as now explained in the answer. Thank you! – Manuel Durando Nov 27 '13 at 15:24