PurpleSlade
asked on
Create an XML document in memory using createDocument(null, null, null) throws error
I am creating an XML document in memory. When it gets to the following code:
DOMImplementation impl = builder.getDOMImplementati on();
// create the root element
org.w3c.dom.Document doc = impl.createDocument(null, null, null);
it throws the following error:
org.w3c.dom.DOMException: INVALID_CHARACTER_ERR: An invalid or illegal XML character is specified.
I have looked on the net and apparently there is an issue with passing null, null, null - for example here:
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4913260
However, it doesn't say how to correct the issue. Any help please? :-)
DOMImplementation impl = builder.getDOMImplementati
// create the root element
org.w3c.dom.Document doc = impl.createDocument(null, null, null);
it throws the following error:
org.w3c.dom.DOMException: INVALID_CHARACTER_ERR: An invalid or illegal XML character is specified.
I have looked on the net and apparently there is an issue with passing null, null, null - for example here:
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4913260
However, it doesn't say how to correct the issue. Any help please? :-)
The second parameter, qualified name of the document element, is NOT allowed to be 'null'
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hmmm, I got the example from here which used null parameters:
http://www.rgagnon.com/javadetails/java-0530.html
What exactly does "qualified name of the document element to be created" mean?
http://www.rgagnon.com/javadetails/java-0530.html
What exactly does "qualified name of the document element to be created" mean?
Document doc = DocumentBuilder.newDocumen t();
Thus spake the HexaGenius ;-)
>>> Hmmm, I got the example from here which used null parameters:
It depends on what version xerces.jar you are using. The one you are using is supposed like this one:
org.apache.xerces.dom.DOMI mplementat ionImpl.cr eateDocume nt() ->
public Document createDocument(String s, String s1, DocumentType documenttype)
throws DOMException
{
if(documenttype != null && documenttype.getOwnerDocum ent() != null)
{
throw new DOMException((short)4, "DOM005 Wrong document");
} else
{
DocumentImpl documentimpl = new DocumentImpl(documenttype) ;
org.w3c.dom.Element element = documentimpl.createElement NS(s, s1);
documentimpl.appendChild(e lement);
return documentimpl;
}
}
Then, org.apache.xerces.dom.Core DocumentIm pl.createE lementNS() ->
public Element createElementNS(String s, String s1)
throws DOMException
{
if(errorChecking && !isXMLName(s1))
throw new DOMException((short)5, "DOM002 Illegal character");
else
return new ElementNSImpl(this, s, s1);
}
Then:
public static boolean isXMLName(String s)
{
if(s == null)
return false;
else
return XMLCharacterProperties.val idName(s);
}
It depends on what version xerces.jar you are using. The one you are using is supposed like this one:
org.apache.xerces.dom.DOMI
public Document createDocument(String s, String s1, DocumentType documenttype)
throws DOMException
{
if(documenttype != null && documenttype.getOwnerDocum
{
throw new DOMException((short)4, "DOM005 Wrong document");
} else
{
DocumentImpl documentimpl = new DocumentImpl(documenttype)
org.w3c.dom.Element element = documentimpl.createElement
documentimpl.appendChild(e
return documentimpl;
}
}
Then, org.apache.xerces.dom.Core
public Element createElementNS(String s, String s1)
throws DOMException
{
if(errorChecking && !isXMLName(s1))
throw new DOMException((short)5, "DOM002 Illegal character");
else
return new ElementNSImpl(this, s, s1);
}
Then:
public static boolean isXMLName(String s)
{
if(s == null)
return false;
else
return XMLCharacterProperties.val
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Correctly spake considering DocumentBuilder represented its instance ;-)
;-)
You can try this:
DOMImplementation impl = builder.getDOMImplementati on();
org.w3c.dom.Document doc = impl.createDocument(null, "XMLRoot", null);
NodeList children = doc.getElementsByTagName(" XMLRoot");
System.out.println("Initia lized" + children.getLength());
DOMImplementation impl = builder.getDOMImplementati
org.w3c.dom.Document doc = impl.createDocument(null, "XMLRoot", null);
NodeList children = doc.getElementsByTagName("
System.out.println("Initia
What exactly does "qualified name of the document element to be created" mean?
- you XML document Root element name
- you XML document Root element name
ASKER
owenli27 - how do you get a handle to the root node once it's been created through that parameter? Is there something like Element root = doc.getRootElement() or the like?
CEHJ - your amended solution compiles fine and there are no more errors - it's probably what I will end up using since it is pretty clean.
mayankeagle - I haven't tried your solution but I am assuming it also works.
CEHJ - your amended solution compiles fine and there are no more errors - it's probably what I will end up using since it is pretty clean.
mayankeagle - I haven't tried your solution but I am assuming it also works.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
OK - that works as well. So, 3 working solutions ... now the point divvying. Thanks everyone for your help!
:-)
ASKER
Hm - I probably should have put owenli27's solution as the accepted since he answered the specific question I had about the method that didn't work ... if you want owenli27 I can try and have that changed, otherwise I will leave it. Let me know. My apologies, for some reason I thought that they would all show as "Assisted" solutions. The new format had me confused since it doesn't explicity state that I was accepting one particular solution.
It really doesn't matter and you can just leave it.
Thanks for rewards.
Thanks for rewards.
>> I haven't tried your solution but I am assuming it also works.
Yes it does :)
Yes it does :)