Ok that works now and it prints 1 for the answer which is correct. Finally!
The different with my other program is I have the following class:
package schemaparser;
import java.io.*;
import javax.xml.transform.stream.StreamSource;
import org.apache.ws.commons.schema.*;
public class XSDParser {
private InputStream is;
private XmlSchemaCollection schemaCol;
private XmlSchema schema;
public XSDParser(String fileName){
try {
is = new FileInputStream(fileName);
schemaCol = new XmlSchemaCollection();
schema = schemaCol.read(new StreamSource(is), null);
} catch (FileNotFoundException ex) {
ex.printStackTrace();
}
}
public void printSchema(){
schema.write(System.out);
}
public int getRootNode(){
//XmlSchemaObjectTable attribsObjTbl = schema.getAttributes();
XmlSchemaObjectTable elementsObjTbl = schema.getElements();
return elementsObjTbl.getCount();
}
}
And the following class:
package schemaparser;
public class XMLSchemaTest {
public static void main(String [] args) {
XSDParser xsdParser = new
XSDParser("C:\\Users\\Patrick\\Documents\\college\\FYP\\xml\\note.xsd");
xsdParser.printSchema();
System.out.println(xsdParser.getRootNode());
}
}
And for some reason here it prints zero!
On Fri, Apr 10, 2009 at 10:33 PM, Benson Margulies <[email protected]>wrote:
> Yes. GetResourceAsStream takes classpath names, not file system
> pathnames. Stick with FileInputStream for those.
>
> More in a minute ...
>
>
> On Fri, Apr 10, 2009 at 5:31 PM, Patrick Kiernan <[email protected]>
> wrote:
> > Ok fair enough. How do you access the elements in the sequence? ie.
> extract
> > their names?
> > I just tried your test case as a java class:
> >
> > package schemaparser;
> >
> > import java.io.InputStream;
> >
> > import javax.xml.transform.stream.StreamSource;
> >
> > import org.apache.ws.commons.schema.XmlSchema;
> > import org.apache.ws.commons.schema.XmlSchemaCollection;
> > import org.apache.ws.commons.schema.XmlSchemaObjectTable;
> >
> >
> > public class XSDSchemaTest2 {
> >
> > public static void main(String [] args){
> >
> > try{
> > InputStream is =
> > XSDSchemaTest2.class.
> >
> >
> getClassLoader().getResourceAsStream("C:\\Users\\Patrick\\Documents\\college\\FYP\\xml\\note.xsd");
> > XmlSchemaCollection schemaCol = new XmlSchemaCollection();
> > XmlSchema schema = schemaCol.read(new StreamSource(is),
> > null);
> > XmlSchemaObjectTable objectTable = schema.getElements();
> > System.out.println(objectTable.getCount());
> > } catch(Exception ex){
> > ex.printStackTrace();
> > }
> >
> > }
> > }
> >
> > When I try and run that I get the following error:
> >
> > org.apache.ws.commons.schema.XmlSchemaException
> > at
> >
> org.apache.ws.commons.schema.XmlSchemaCollection.read(XmlSchemaCollection.java:386)
> > at
> >
> org.apache.ws.commons.schema.XmlSchemaCollection.read(XmlSchemaCollection.java:422)
> > at
> >
> org.apache.ws.commons.schema.XmlSchemaCollection.read(XmlSchemaCollection.java:448)
> > at schemaparser.XSDSchemaTest2.main(XSDSchemaTest2.java:23)
> >
> > Any idea what's wrong there?
> >
> > Thanks,
> > Patrick
> >
> > On Fri, Apr 10, 2009 at 10:26 PM, Benson Margulies <
> [email protected]>wrote:
> >
> >> Oh! I completely misunderstood the question.
> >>
> >> Only top-level elements go into the collections. The elements inside a
> >> sequence do not. So those elements are not supposed to be in items or
> >> the elements object table. After all, you could have seven different
> >> elements named 'to' inside of seven different complex types.
> >>
> >>
> >>
> >> On Fri, Apr 10, 2009 at 5:22 PM, Patrick Kiernan <[email protected]>
> >> wrote:
> >> > Yes I just saw your commit and the file you uploaded is the full copy
> of
> >> > notes.xsd:
> >> > +<?xml version="1.0"?>
> >> > +<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
> >> > +
> >> > +<xs:element name="note">
> >> > + <xs:complexType>
> >> > + <xs:sequence>
> >> > + <xs:element name="to" type="xs:string"/>
> >> > + <xs:element name="from" type="xs:string"/>
> >> > + <xs:element name="heading" type="xs:string"/>
> >> > + <xs:element name="body" type="xs:string"/>
> >> > + </xs:sequence>
> >> > + </xs:complexType>
> >> > +</xs:element>
> >> > +
> >> > +</xs:schema>
> >> >
> >> >
> >> > On Fri, Apr 10, 2009 at 10:18 PM, Benson Margulies <
> >> [email protected]>wrote:
> >> >
> >> >> I don't have a full copy of 'notes.xsd'. I just have the fragment
> from
> >> >> your first message. To give me a whole file, make a JIRA and attach
> >> >> it. I just committed my unit test: grab the source tree from svn and
> >> >> look at
> >> >>
> >> >> /XmlSchema/src/test/java/tests/SingleElementNoNamespace.java
> >> >>
> >> >> The svn path you want is
> >> >>
> >> >>
> >> >>
> >>
> http://svn.apache.org/repos/asf/webservices/commons/branches/modules/XmlSchema/1_4_X_BRANCH
> >> >>
> >> >>
> >> >>
> >> >>
> >> >>
> >> >> On Fri, Apr 10, 2009 at 5:12 PM, Patrick Kiernan <[email protected]
> >
> >> >> wrote:
> >> >> > JDK is 6
> >> >> > Don't have anything special in my classpath:
> >> >> > .;C:\Program Files\Java\jre6\lib\ext\QTJava.zip;C:\program
> >> >> > files\java\jdk1.6.0_10\lib
> >> >> >
> >> >> > I don't have maven so I can't make a self-contained failing case
> using
> >> >> > maven.
> >> >> >
> >> >> > Is one member not incorrect since note.xsd has 5 elements:
> >> >> >
> >> >> > note
> >> >> > to
> >> >> > from
> >> >> > heading
> >> >> > body
> >> >> >
> >> >> > Can you provide your code please?
> >> >> >
> >> >> > Thanks,
> >> >> > Patrick
> >> >> >
> >> >> > On Fri, Apr 10, 2009 at 10:02 PM, Benson Margulies <
> >> >> [email protected]>wrote:
> >> >> >
> >> >> >> I just turned your code into a unit test, and it worked. The items
> >> >> >> collection comes up with one member, as does the elements object
> >> >> >> table.
> >> >> >>
> >> >> >> So, we have to look for some more interesting explanation. What
> JDK?
> >> >> >> Anything fancy in your class path, like a specific version of
> Xerces
> >> >> >> or some other alternative parser?
> >> >> >>
> >> >> >> Can you make a self-contained failing case using maven to specify
> all
> >> >> >> the dependencies?
> >> >> >>
> >> >> >> It is remotely possible that you've hit something fixed since
> 1.4.4,
> >> >> >> but very little has changed since then, and nothing in this
> >> >> >> neighborhood.
> >> >> >>
> >> >> >>
> >> >> >> On Fri, Apr 10, 2009 at 4:38 PM, Patrick Kiernan <
> [email protected]
> >> >
> >> >> >> wrote:
> >> >> >> > I assumes that's:
> >> >> >> >
> >> >> >> > XmlSchemaObjectTable elementsObjTbl = schema.getElements();
> >> >> >> > System.out.println(elementsObjTbl.getCount());
> >> >> >> >
> >> >> >> > That's also empty.
> >> >> >> >
> >> >> >> > On Fri, Apr 10, 2009 at 9:27 PM, Benson Margulies <
> >> >> [email protected]
> >> >> >> >wrote:
> >> >> >> >
> >> >> >> >> I would expect that code of yours to work. My only suggestion
> >> before
> >> >> I
> >> >> >> >> debug is that you get the element collection and see if THAT is
> >> also
> >> >> >> >> empty.
> >> >> >> >>
> >> >> >> >>
> >> >> >> >> On Fri, Apr 10, 2009 at 2:50 PM, Patrick Kiernan <
> >> [email protected]
> >> >> >
> >> >> >> >> wrote:
> >> >> >> >> > Cool, thanks :)
> >> >> >> >> > Is it a bug or?
> >> >> >> >> >
> >> >> >> >> > Maybe you could include some code showing the common uses of
> the
> >> >> API.
> >> >> >> >> >
> >> >> >> >> > I basically want to be able to extract the elements of a
> schema
> >> and
> >> >> >> then
> >> >> >> >> > display it using a
> >> >> >> >> > JTree.
> >> >> >> >> >
> >> >> >> >> > Patrick
> >> >> >> >> >
> >> >> >> >> > On Fri, Apr 10, 2009 at 7:24 PM, Benson Margulies <
> >> >> >> [email protected]
> >> >> >> >> >wrote:
> >> >> >> >> >
> >> >> >> >> >> I'm a bit buried under Passover, but I'll sort this out over
> >> the
> >> >> >> >> >> weekend some time.
> >> >> >> >> >>
> >> >> >> >> >> On Fri, Apr 10, 2009 at 11:46 AM, Patrick Kiernan <
> >> >> >> [email protected]>
> >> >> >> >> >> wrote:
> >> >> >> >> >> > Version 1.4.4
> >> >> >> >> >> >
> >> >> >> >> >> > On Fri, Apr 10, 2009 at 1:55 PM, Benson Margulies <
> >> >> >> >> [email protected]
> >> >> >> >> >> >wrote:
> >> >> >> >> >> >
> >> >> >> >> >> >> You don't have a target namespace.... but that should be
> OK.
> >> >> What
> >> >> >> >> >> >> version have you grabbed?
> >> >> >> >> >> >>
> >> >> >> >> >> >> On Fri, Apr 10, 2009 at 8:11 AM, Patrick Kiernan <
> >> >> >> [email protected]
> >> >> >> >> >
> >> >> >> >> >> >> wrote:
> >> >> >> >> >> >> > Hi,
> >> >> >> >> >> >> > Trying to start using the API. Following the
> instructions
> >> in
> >> >> the
> >> >> >> >> >> tutorial
> >> >> >> >> >> >> I
> >> >> >> >> >> >> > can get the schema to print fine.
> >> >> >> >> >> >> >
> >> >> >> >> >> >> > However now I would like to start accessing elements.
> >> >> >> >> >> >> >
> >> >> >> >> >> >> > I tried the following:
> >> >> >> >> >> >> >
> >> >> >> >> >> >> >
> >> >> >> >> >> >> > InputStream is = new FileInputStream("note.xsd");
> >> >> >> >> >> >> > XmlSchemaCollection schemaCol = new
> XmlSchemaCollection();
> >> >> >> >> >> >> > XmlSchema schema = schemaCol.read(new StreamSource(is),
> >> >> null);
> >> >> >> >> >> >> >
> >> >> >> >> >> >> >
> >> >> >> >> >> >> > XmlSchemaObjectTable objectTable =
> schema.getElements();
> >> >> >> >> >> >> >
> >> >> >> >> >> >> > System.out.println(objectTable.getCount());
> >> >> >> >> >> >> >
> >> >> >> >> >> >> >
> >> >> >> >> >> >> > This prints out 0. Should it not print out the number
> of
> >> >> >> elements?
> >> >> >> >> >> >> >
> >> >> >> >> >> >> >
> >> >> >> >> >> >> > notes.xsd is as follows:
> >> >> >> >> >> >> >
> >> >> >> >> >> >> >
> >> >> >> >> >> >> >
> >> >> >> >> >> >> > <?xml version="1.0"?>
> >> >> >> >> >> >> > <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema
> ">
> >> >> >> >> >> >> >
> >> >> >> >> >> >> > <xs:element name="note">
> >> >> >> >> >> >> > <xs:complexType>
> >> >> >> >> >> >> > <xs:sequence>
> >> >> >> >> >> >> > <xs:element name="to" type="xs:string"/>
> >> >> >> >> >> >> > <xs:element name="from" type="xs:string"/>
> >> >> >> >> >> >> > <xs:element name="heading" type="xs:string"/>
> >> >> >> >> >> >> > <xs:element name="body" type="xs:string"/>
> >> >> >> >> >> >> > </xs:sequence>
> >> >> >> >> >> >> > </xs:complexType>
> >> >> >> >> >> >> > </xs:element>
> >> >> >> >> >> >> >
> >> >> >> >> >> >> > </xs:schema>
> >> >> >> >> >> >> >
> >> >> >> >> >> >> >
> >> >> >> >> >> >> >
> >> >> >> >> >> >> > Thanks,
> >> >> >> >> >> >> >
> >> >> >> >> >> >> > Patrick
> >> >> >> >> >> >> >
> >> >> >> >> >> >>
> >> >> >> >> >> >
> >> >> >> >> >>
> >> >> >> >> >
> >> >> >> >>
> >> >> >> >
> >> >> >>
> >> >> >
> >> >>
> >> >
> >>
> >
>