Clicky

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

asked 05/04/2011 01:15

hamsalgla's gravatar image

hamsalgla ♦♦


18 Answers:
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.
link
for_yan's gravatar image

for_yan

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
link
for_yan's gravatar image

for_yan

Thanks for the quick response

I follow your instructions creating the main class:

 
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
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"));
    }
}


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

 
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
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() {
    }
}


And this was the output:

 
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
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)


Any suggestions?? :(
link
hamsalgla's gravatar image

hamsalgla

your missing a dependency
xsom requires relaxng
link
objects's gravatar image

objects

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
link
hamsalgla's gravatar image

hamsalgla

When there is problem with method finding symbol not a class it is usually becaouse version mismatch
link
for_yan's gravatar image

for_yan

>                 //if( e.isAbstract() )

should be:


                if( e.getAbstract() )
link
objects's gravatar image

objects

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

link
for_yan's gravatar image

for_yan

Oh, if it was something modfiable in the  code, then another story
link
for_yan's gravatar image

for_yan

?? 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
link
objects's gravatar image

objects

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
link
hamsalgla's gravatar image

hamsalgla

> 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?
link
objects's gravatar image

objects

Hi objects

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

Thanks again
link
hamsalgla's gravatar image

hamsalgla

thanks mate
link
objects's gravatar image

objects

Your answer
[hide preview]

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Tags:

×11
×2
×2

Asked: 05/04/2011 01:15

Seen: 948 times

Last updated: 05/06/2011 05:47