• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

How to use DomParser.parse() to parse an XML String instead of the XML location?

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I am trying to parse an xml against a xml schema using xerces.
I have encountered "java.net.MalformedURLException: no protocol:" when I tried to do DomParser.parse("<memories><memory tapeid="23412"><subdate>5/23/2001</subdate><donor>John Baker</donor><subject>Fishing off Pier 60</subject></memory></memories>").
I read from the forum and know that I cannot do this. I have to parse a "Xml location". However, I do not have a file location for it. My program is to type the xml data in JTextArea and then validate it against a schema. In my case, I do not have a xml location, but a xml string.
Can anyone please advise me what I should do to make this work?
Thanks,
Claudia
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try this:
 
Claudia Low
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Thanks. This works but it leads to another error.
I have
"Parsing error: cvc-elt.1: Cannot find the declaration of element 'memories'."
Actually, my java code is
public void validateMemory()
{
try {
String filename = "my/editor/memory1.xsd";
ClassLoader cl = this.getClass().getClassLoader();
URL url = cl.getResource(filename);
if (url == null) {
System.out.println("cannot find memory1.xsd.");
throw new FileNotFoundException(filename);
}
final InputStream in = url.openStream();
DOMParser parser = new DOMParser();
parser.setEntityResolver( new EntityResolver() {
public InputSource resolveEntity(String publicId, String systemId) {
// use the default behaviour
return new InputSource(in);
}});
if (in != null){
in.close();
}
parser.setFeature("http://xml.org/sax/features/validation", true);
parser.setFeature("http://apache.org/xml/features/validation/schema", true);
ErrorChecker errors = new ErrorChecker();
parser.setErrorHandler(errors);
parser.parse( new InputSource(new StringReader(codePanel.getText())));
} catch (Exception e) {
System.out.print("Problem parsing the file." + e.toString());
}
}

my xml is
<?xml version="1.0" encoding="UTF-8"?>
<memories>
<memory tapeid="23412">
<subdate>5/23/2001</subdate>
<donor>John Baker</donor>
<subject>Fishing off Pier 60</subject>
</memory>
</memories>
and schema is
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="memories">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="memory" type="memoryType" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:complexType name="memoryType">
<xsd:sequence>
<xsd:element name="subdate" type="xsd:string"/>
<xsd:element name="donor" type="xsd:string"/>
<xsd:element name="subject" type="xsd:string"/>
</xsd:sequence>
<xsd:attribute name="tapeid" type="xsd:int"/>
</xsd:complexType>
</xsd:schema>

Is my schema not loaded properly?
I am quite new here but if I set schema in the entityresolver in DomParser, how does DomParser knows that this is the schema to validate against my document? or should I also code? :
parser.setProperty("http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation", "memory.xsd");
thanks for your help.
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i think u should use parser.setProperty("http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation", "memory.xsd");
 
Claudia Low
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I still get this exception "Cannot find the declaration of element 'memories'"
 
Claudia Low
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have made another test. The parser.parse(new InputSource(new StringReader(codePanel.getTExt()))); works when I put memory1.xsd in the correct specific location (C:\my\editor directory), using
parser.setProperty("http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation","memory1.xsd");
However, because my program and schema will need to be in a jar file. so, I have to use a entityresovler to load the xsd. Is the way I load entityresolver correct?
Any help is appreciated.
Claudia
 
Claudia Low
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have found the solution. Just to share with you.
In the java code, you set the entityresolver to the schema URI. You don't have to put in parser.setProperty.
parser.setEntityResolver(new EntityResolver() {
public InputSource resolveEntity(String publicID, String systemID)
throws IOException, SAXException {
URL schemaURL = null;
schemaURL = this.getClass().getClassLoader().getResource("my/editor/BPSS.xsd");
InputStream inp = schemaURL.openStream();
return new InputSource(schemaURL.openStream());
}});

However, in your xml, you have to define the noNamespaceSchemaLocation to that of URI of entityresolver. Then the resolver know which schema to validate.
<memories xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:noNamespaceSchemaLocation=my/editor/BPSS.xsd'>
This works.
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for sharing the solution with us, Claudia!
 
reply
    Bookmark Topic Watch Topic
  • New Topic