• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2205
  • Last Modified:

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
0
hamsalgla
Asked:
hamsalgla
  • 8
  • 6
  • 4
6 Solutions
 
objectsCommented:
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
0
 
for_yanCommented:
Yes you need to create project in Netbeans, add jar file conatining
xsom library to dependencies and create class paste there
the code, but you need of course to follow general java rules - to have
main method to start execution, configure Run environemnt
and do as you do HelloWorld project, which I hope you did.
Yes, the difference is that you have too have additional jar as a depnedency.
0
 
objectsCommented:
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
for_yanCommented:
to add a depnedency in Netbeans, you may right-click your project
in left-side column wghere you have all projects,
then go to Propertties, then select Libraries in the pop-pu winodw
in its left colimn, then click Add Jar/Folder and browse to the jar which you would have downloaded
with your XSOM library
0
 
hamsalglaAuthor Commented:
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?? :(
0
 
objectsCommented:
your missing a dependency
xsom requires relaxng
0
 
hamsalglaAuthor Commented:
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
0
 
for_yanCommented:
When there is problem with method finding symbol not a class it is usually becaouse version mismatch
0
 
objectsCommented:
>                 //if( e.isAbstract() )

should be:


                if( e.getAbstract() )
0
 
for_yanCommented:
Which xerces.jar do you have?

Maybe try to add this at the beginning of your classpath
http://mirrors.ibiblio.org/pub/mirrors/maven/activesoap/jars/jaxb-xercesImpl-1.5.jar

0
 
for_yanCommented:
Oh, if it was something modfiable in the  code, then another story
0
 
objectsCommented:
?? 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
0
 
hamsalglaAuthor Commented:
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
0
 
objectsCommented:
> 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?
0
 
hamsalglaAuthor Commented:
Hi objects

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

Thanks again
0
 
objectsCommented:
thanks mate
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

  • 8
  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now