Hello: I want to know why there is a HTTP 500 error if there is the last two rows code: //------------------------------------ BasicDocument doc = new BasicDocument(); BasicElement rootElement = new BasicElement(doc, “rootElement”);
BasicElement node1 = new BasicElement(doc,“node1”); rootElement.appendChild(node1);
BasicAttribute node1attr = new BasicElement(doc,“node1attr”,“attrvalue”); node1.appendChild(node1attr); //---------------------------------
[This message was edited by wangsir on 11 Mar 2002 at 09:12.]
The last two lines look a little strange: BasicAttribute node1attr = new BasicElement(doc,“node1attr”,“attrvalue”);
Is this correct? Should the instantiation be of a “new BasicAttribute” too?
(This looks likely to cause a problem at some point - instantiating a BasicElement into a BasicAttribute field…)
Also, I am not sure, but I think that appendChild() may be the wrong way to set an attribute. Isn’t Element.setAttribute(String name, String value) better - or even Element.setAttributeNode(org.w3c.dom.Attr)?
Perhaps someone more familiar with DOM can comment on this…
If you are appending a node as an attribute it should be an instance of an attribute not an element. When the parser checks the nodetype it will get a ELEMENT_NODE type, therefore it will just append an element to your parent node instead of an attribute. Usually, the Element object has a setAttribute method you can use. If you really need to do it by appending, create the instance using the constructor of an attribute node, not an element.
Sorry,sorry for my carelessness,and Trevor is right.I should have written to “new BasicAttribute”,sorry! According to what Jfarjona said,I solved it,thanks!