[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Parsing XML Schema in Java - with netbeans

Posted on 2011-05-04
19
Medium Priority
?
2,057 Views
Last Modified: 2013-12-14
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
Comment
Question by:hamsalgla
  • 8
  • 6
  • 4
18 Comments
 
LVL 92

Expert Comment

by:objects
ID: 35691855
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
 
LVL 47

Assisted Solution

by:for_yan
for_yan earned 1332 total points
ID: 35691864
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
 
LVL 92

Assisted Solution

by:objects
objects earned 668 total points
ID: 35691878
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 47

Assisted Solution

by:for_yan
for_yan earned 1332 total points
ID: 35691922
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
 

Author Comment

by:hamsalgla
ID: 35692364
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
 
LVL 47

Accepted Solution

by:
for_yan earned 1332 total points
ID: 35692393
0
 
LVL 92

Expert Comment

by:objects
ID: 35692420
your missing a dependency
xsom requires relaxng
0
 
LVL 92

Expert Comment

by:objects
ID: 35692425
0
 

Author Comment

by:hamsalgla
ID: 35692511
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
 
LVL 47

Expert Comment

by:for_yan
ID: 35692611
When there is problem with method finding symbol not a class it is usually becaouse version mismatch
0
 
LVL 92

Assisted Solution

by:objects
objects earned 668 total points
ID: 35692644
>                 //if( e.isAbstract() )

should be:


                if( e.getAbstract() )
0
 
LVL 47

Assisted Solution

by:for_yan
for_yan earned 1332 total points
ID: 35692645
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
 
LVL 47

Expert Comment

by:for_yan
ID: 35692661
Oh, if it was something modfiable in the  code, then another story
0
 
LVL 92

Expert Comment

by:objects
ID: 35692969
?? 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
 

Author Comment

by:hamsalgla
ID: 35693220
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
 
LVL 92

Expert Comment

by:objects
ID: 35694991
> 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
 

Author Comment

by:hamsalgla
ID: 35695763
Hi objects

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

Thanks again
0
 
LVL 92

Expert Comment

by:objects
ID: 35710938
thanks mate
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

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…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
Suggested Courses
Course of the Month20 days, 7 hours left to enroll

868 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