Link to home
Start Free TrialLog in
Avatar of hamsalgla
hamsalglaFlag for Australia

asked on

Parsing XML Schema in Java - with netbeans

Hi all,

I'm new in java development

I'm trying to parse XML Schema (XSD file) using XSOM Library found here
I import the library and try to use a sample code found here to parse XSD file to get information about elements and attributes in that file.

What should I do first?
Do I need to create a new java application or class?

Many thanks in advance
Avatar of Mick Barry
Mick Barry
Flag of Australia image

yes create a new application/class
theres an example here
http://it.toolbox.com/blogs/enterprise-web-solutions/parsing-an-xsd-schema-in-java-32565
SOLUTION
Avatar of for_yan
for_yan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of hamsalgla

ASKER

Thanks for the quick response

I follow your instructions creating the main class:

 
package schemaapp;
import java.io.File;

public class SchemaApp {

    public static void main(String[] args) {
        System.out.println("Hello World!");
        
        XSDParser xs = new XSDParser();
        xs.parse(new File("shiporder.xsd"));
    }
}

Open in new window


And then the XSDParser class which I already created like this:

 
package schemaapp;

import com.sun.org.apache.xerces.internal.impl.xs.XSElementDecl;
import com.sun.xml.xsom.XSSchema;
import com.sun.xml.xsom.XSSchemaSet;
import com.sun.xml.xsom.parser.XSOMParser;
import java.io.File;
import java.util.Iterator;

public class XSDParser {

    public void parse(File file){
    try {
        XSOMParser parser = new XSOMParser();
        parser.parse(file);
        XSSchemaSet sset = parser.getResult();
        
        // iterate each XSSchema object. XSSchema is a per-namespace schema.
        Iterator itr = sset.iterateSchema();
        while( itr.hasNext() ) {
        XSSchema s = (XSSchema)itr.next();
  
        System.out.println("Target namespace: "+s.getTargetNamespace());
  
        Iterator jtr = s.iterateElementDecls();
            while( jtr.hasNext() ) {
                XSElementDecl e = (XSElementDecl)jtr.next();
    
                System.out.print( e.getName() );
                //if( e.isAbstract() )
                //    System.out.print(" (abstract)");
                System.out.println();
            }
        } 
    }
    catch (Exception exp) {
        exp.printStackTrace(System.out);
    }
}
    
    public XSDParser() {
    }
}

Open in new window


And this was the output:

 
run:
Hello World!
Exception in thread "main" java.lang.NoClassDefFoundError: org/relaxng/datatype/ValidationContext
	at com.sun.xml.xsom.impl.parser.ParserContext.newNGCCRuntime(ParserContext.java:154)
	at com.sun.xml.xsom.impl.parser.ParserContext.parse(ParserContext.java:128)
	at com.sun.xml.xsom.impl.parser.ParserContext.<init>(ParserContext.java:100)
	at com.sun.xml.xsom.parser.XSOMParser.<init>(XSOMParser.java:110)
	at com.sun.xml.xsom.parser.XSOMParser.<init>(XSOMParser.java:84)
	at schemaapp.XSDParser.parse(XSDParser.java:22)
	at schemaapp.SchemaApp.main(SchemaApp.java:22)
Caused by: java.lang.ClassNotFoundException: org.relaxng.datatype.ValidationContext
	at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
	... 7 more
Java Result: 1
BUILD SUCCESSFUL (total time: 3 seconds)

Open in new window


Any suggestions?? :(
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
your missing a dependency
xsom requires relaxng
Thanks a lot. You are the man :)

Last question: When I activate the statement in lines: 30 & 31 in the XSDParser class, It gives me an error saying:

cannot find symbol
symbol:   method isAbstract()
location: variable e of type com.sun.org.apache.xerces.internal.impl.xs.XSElementDec

What is the problem

Thanks again
When there is problem with method finding symbol not a class it is usually becaouse version mismatch
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Oh, if it was something modfiable in the  code, then another story
?? please explain why you accepted that comment ?

The class you were missing was certainly not in that jar
and you've ignore all the help I gave you
Hi objects
I think you misunderstand what I did
I think the best solution that solve my problem was in this jar file (from for_yan)
http://mirrors.ibiblio.org/pub/mirrors/maven2/com/sun/xml/bind/jaxb-xjc/2.1.7/jaxb-xjc-2.1.7.jar

So I choose it to be the solution to my problem.

I have not ignore all your help but some of them:
"yes create a new application/class
theres an example here
http://it.toolbox.com/blogs/enterprise-web-solutions/parsing-an-xsd-schema-in-java-32565"
Is already embedded in my first question.

"your missing a dependency
xsom requires relaxng"
I think, RelaxNG is somthing different than XML Schema. I ignore it because I don't need it.

But you help in the rest of replies

To be honest, this is the first time for me in this forum, and I don't know how to choose more than one solution :(

Thanks again
> I think, RelaxNG is somthing different than XML Schema. I ignore it because I don't need it.

you should have it though. xsom relies on it. WHat you have done is change the parser used
not sure if thats what you actually wanted to do

I also showed you how to start a netbeans project
and helped with your compiler error

> To be honest, this is the first time for me in this forum, and I don't know how to choose more than one solution :(

would you like me to show you how?
Hi objects

If you can show me how to do it, I'll be happy.

Thanks again
thanks mate