Solved

Create an XML document in memory using createDocument(null, null, null) throws error

Posted on 2007-03-27
18
1,022 Views
Last Modified: 2007-12-19
I am creating an XML document in memory.  When it gets to the following code:

                        DOMImplementation impl = builder.getDOMImplementation();

                        // 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?  :-)
0
Comment
Question by:PurpleSlade
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 4
  • +1
18 Comments
 
LVL 9

Expert Comment

by:owenli27
ID: 18800968
The second parameter, qualified name of the document element,  is NOT allowed to be 'null'

0
 
LVL 30

Assisted Solution

by:Mayank S
Mayank S earned 100 total points
ID: 18801074
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance () ;
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder () ;
StringReader reader = new StringReader ( "<ROOT/>" ) ;
Document doc = docBuilder.parse ( new InputSource ( reader ) ) ;
reader.close () ;

- gives you a blank document with a root node. You can append children to the root node thereafter
0
 
LVL 2

Author Comment

by:PurpleSlade
ID: 18801092
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?
0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 86

Expert Comment

by:CEHJ
ID: 18801112
Document doc = DocumentBuilder.newDocument();
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 18801157
Thus spake the HexaGenius ;-)
0
 
LVL 9

Expert Comment

by:owenli27
ID: 18801288
>>> 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.DOMImplementationImpl.createDocument() ->
public Document createDocument(String s, String s1, DocumentType documenttype)
        throws DOMException
    {
        if(documenttype != null && documenttype.getOwnerDocument() != null)
        {
            throw new DOMException((short)4, "DOM005 Wrong document");
        } else
        {
            DocumentImpl documentimpl = new DocumentImpl(documenttype);
            org.w3c.dom.Element element = documentimpl.createElementNS(s, s1);
            documentimpl.appendChild(element);
            return documentimpl;
        }
    }

Then, org.apache.xerces.dom.CoreDocumentImpl.createElementNS() ->
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.validName(s);
    }

0
 
LVL 86

Accepted Solution

by:
CEHJ earned 200 total points
ID: 18801305
Spake wrongly - should have been

Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 18801332
Correctly spake considering DocumentBuilder represented its instance ;-)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 18801344
;-)
0
 
LVL 9

Expert Comment

by:owenli27
ID: 18801366
You can try this:
DOMImplementation impl = builder.getDOMImplementation();
org.w3c.dom.Document doc = impl.createDocument(null, "XMLRoot", null);

NodeList children = doc.getElementsByTagName("XMLRoot");
System.out.println("Initialized" + children.getLength());
0
 
LVL 9

Expert Comment

by:owenli27
ID: 18801380
What exactly does "qualified name of the document element to be created" mean?
- you XML document Root element name
0
 
LVL 2

Author Comment

by:PurpleSlade
ID: 18801550
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.

0
 
LVL 9

Assisted Solution

by:owenli27
owenli27 earned 200 total points
ID: 18801603
Document doc = implementation.createDocument(null, "MyXMLRoot", null);
Element root = doc.getDocumentElement();
System.out.println("Root Element Name = " + root.getNodeName());
0
 
LVL 2

Author Comment

by:PurpleSlade
ID: 18801628
OK - that works as well.  So, 3 working solutions ... now the point divvying.  Thanks everyone for your help!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 18801680
:-)
0
 
LVL 2

Author Comment

by:PurpleSlade
ID: 18801793
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.  
0
 
LVL 9

Expert Comment

by:owenli27
ID: 18801914
It really doesn't matter and you can just leave it.  
Thanks for rewards.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 18802320
>> I haven't tried your solution but I am assuming it also works.

Yes it does :)
0

Featured Post

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question