aspose file tools*
The moose likes XML and Related Technologies and the fly likes How to use DomParser.parse() to parse an XML String instead of the XML location? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "How to use DomParser.parse() to parse an XML String instead of the XML location?" Watch "How to use DomParser.parse() to parse an XML String instead of the XML location?" New topic
Author

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

Claudia Low
Greenhorn

Joined: Sep 10, 2003
Posts: 5
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
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Try this:


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Claudia Low
Greenhorn

Joined: Sep 10, 2003
Posts: 5
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.
Howie Jiang
Greenhorn

Joined: Aug 25, 2003
Posts: 26
i think u should use parser.setProperty("http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation", "memory.xsd");
Claudia Low
Greenhorn

Joined: Sep 10, 2003
Posts: 5
Hi, I still get this exception "Cannot find the declaration of element 'memories'"
Claudia Low
Greenhorn

Joined: Sep 10, 2003
Posts: 5
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

Joined: Sep 10, 2003
Posts: 5
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
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Thanks for sharing the solution with us, Claudia!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to use DomParser.parse() to parse an XML String instead of the XML location?