Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XML Validation with javax.xml.validation

Status
Not open for further replies.

MoreFeo

Technical User
Nov 29, 2002
547
ES
Hi, I'm trying to create a Web Service that validates an XML against an xsd file.

I've seen an example ( that uses javax.xml.validation against an org.w3c.dom.Document to validate.

The problem is that the Web Service receives the XML as a String, and I'm having errors when creating the Document.

This is what I have:
Code:
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import javax.xml.parsers.*;
import org.w3c.dom.Document;

...

public String validateXML(String stringXML)
{
   try
   {
      byte byteXML[] = stringXML.getBytes();
      InputStream isXML = new ByteArrayInputStream(byteXML);
      DocumentBuilder parserXML = DocumentBuilderFactory.newInstance().newDocumentBuilder();
      Document documentXML = parserXML.parse(isXML);

      ...
   }catch(Exception e){}
}

I get an error at line Document documentXML = parserXML.parse(isXML);

It gives
java.io.UTFDataFormatException: Invalid byte 2 of 2-byte UTF-8 sequence.

Any ideas why it is giving this error?

Thanks
 
Ok, I've got it.

Code:
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            Document documentoXML = dbf.newDocumentBuilder().parse(new InputSource(new StringReader(stringXML)));

Much easier than I was doing.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top