Skip to main content

Error java.lang.IllegalArgumentException tyring to build an XML document

2 replies [Last post]
Joined: 2008-04-11
Points: 0

Hello, I'm having a problem with building an XML document while trying to use JAXP DocumentBuilderFactoryImpl.setAttribute

The error is:
at org.apache.xerces.jaxp.DocumentBuilderFactoryImpl.setAttribute(

I am using xerces-2_9_1, apache-tomcat-6.0.10, Java jdk1.5.0_11.

I have tried putting the xercesImpl.jar, xercesSamples.jar, and xml-apis.jar in my Classpath, in Tomcat's lib directory, in my Eclipse Project directory lib directory.

I have also tried different versions of the Xerces implementations, namely just the xerces.jar.

I have tried a different JDK.

I am really stumped. As you can tell, I'm pretty new to XML document creatiion and validation.

Here's an example of my Java class that throws the java.lang.IllegalArgumentException. The bold line is where the code bombs.

package xml;

import util.TempoUtilities;
import java.util.Calendar;
import javax.xml.parsers.*;
import org.apache.xerces.parsers.DOMParser;
import org.apache.xml.serialize.*;
import org.w3c.dom.*;

private DocumentBuilderFactory getDocumentBuilderFactory()
if(i_objDocumentBuilderFactory == null)
System.setProperty("javax.xml.parsers.DocumentBuilderFactory", "org.apache.xerces.jaxp.DocumentBuilderFactoryImpl");

i_objDocumentBuilderFactory= new org.apache.xerces.jaxp.DocumentBuilderFactoryImpl();


//this is where it bombs
i_objDocumentBuilderFactory.setAttribute("", "");
i_objDocumentBuilderFactory.setAttribute("", Boolean.TRUE);
i_objDocumentBuilderFactory.setAttribute("", Boolean.TRUE);
i_objDocumentBuilderFactory.setAttribute("", Boolean.TRUE);
i_objDocumentBuilderFactory.setAttribute(getSchemaPropertyName(), getSchemaPropertyValue());
return i_objDocumentBuilderFactory;

Thanks so much for any insight or help into figuring this out. I have a feeling I'm having jar conflicts, I'm just not sure what I'm supposed to have and what version.


Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2008-04-11
Points: 0

Actually, I think I just fixed this by researching a bit more and adding

import javax.xml.parsers.DocumentBuilderFactory;

to my import statements.

That seemed to work!


Joined: 2004-12-15
Points: 0

Hi Melodie,

Glad you made it work. But I noticed a couple of things that might be useful to you.

JAXP is designed with a pluggability layer, which means that users are free to choose or move to any implementation they prefer. The JDK comes with a Sun implementation. But you are free to plug in any implementation, such as Xerces as you did.

The DocumentBuilderFactory, for example, finds the concrete subclass using the following order:

1. The value of a system property, which you did
2. The contents of the file $JAVA_HOME/lib/
3. The Jar Service Provider discovery mechanism specified in the Jar File Specification. A jar file can have a resource such as META-INF/services/javax.xml.parsers.DocumentBuilderFactory containing the name of the concrete class to instantiate.
4. The fallback platform default implementation.

Having specified the implementation class, as you did by setting the system property:
System.setProperty("javax.xml.parsers.DocumentBuilderFactory", "org.apache.xerces.jaxp.DocumentBuilderFactoryImpl");

You're telling the DocumentBuilderFactory to use Xerces. So instead of referring to the implementation-specific class, DocumentBuilderFactoryImpl, you may get a factory instance by calling the newInstance method, e.g.
dbf = DocumentBuilderFactory.newInstance();

Run this with -verbose option, you'll see that the Xerces classes shall be loaded after setting the above property, otherwise the Sun default implementation in the JDK.