IMPORTANT:
this is not a Support Forum! Experienced users might answer from time to time questions posted here. If you need a professional and reliable answer, or if you want to report a bug, please contact Altova Support instead.

Profile: charpel
About
User Name: charpel
Forum Rank: Newbie
Real Name:
Location
Occupation:
Interests:
Gender: None Specified
Statistics
Joined: Friday, March 18, 2016
Last Visit: Sunday, January 31, 2021 4:11:33 PM
Number of Posts: 1
[0.01% of all post / 0.00 posts per day]
Avatar
Last 10 Posts
Topic: Raptor Java API Example
Posted: Friday, March 18, 2016 3:38:28 PM
I've been struggling to get a simple Java example working with an external schema. Below is my code. Can anyone see anything wrong with it? XMLSpy 2016 says the XML is valid, but running the Java code always yields NOT valid:

The Java code:

Code:
import com.altova.raptorxml.RaptorXML;
import com.altova.raptorxml.RaptorXMLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.altova.raptorxml.RaptorXMLFactory;
import com.altova.raptorxml.XMLValidator;
import com.altova.raptorxml.XMLValidator.ENUMValidationType;

public class RaptorXMLValidator {

    private static final Logger LOG = Logger.getLogger (RaptorXMLValidator.class.getName ());
    private final RaptorXMLFactory rxml = RaptorXML.getFactory ();

    public static void main (String[] a) {
        RaptorXMLValidator validator = new RaptorXMLValidator ();
        String schemaPath = "/home/foo/raptortest.xsd";
        String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><RAPTOR_TEST xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"><ELEMENT_1>text</ELEMENT_1><ELEMENT_2>text</ELEMENT_2></RAPTOR_TEST>";
       
        if (validator.validateXml (schemaPath, xml)) {
            LOG.info ("XML is valid!");
        } else {
            LOG.info ("XML is *NOT* valid!");
        }
    }

    public boolean validateXml (String schemaPath, String xml) {

        boolean isValid = true;

        try {
            // Create a Raptor XMLValidator instance
            XMLValidator validator = rxml.getXMLValidator();
            validator.setSchemaFileName (schemaPath);
            validator.setInputXMLFromText (xml);

            // Validating the XML against the schema
            isValid = validator.isValid (ENUMValidationType.eValidateXMLWithXSD);

        } catch (RaptorXMLException ex) {
            Logger.getLogger (RaptorXMLValidator.class.getName()).log (Level.SEVERE, null, ex);
        }

        return isValid;

    }

}

And the contents of "/home/foo/raptortest.xsd" is:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning" elementFormDefault="qualified" attributeFormDefault="unqualified" vc:minVersion="1.1">
        <xs:element name="RAPTOR_TEST">
                <xs:complexType>
                        <xs:sequence>
                                <xs:element name="ELEMENT_1"/>
                                <xs:element name="ELEMENT_2"/>
                        </xs:sequence>
                </xs:complexType>
        </xs:element>
</xs:schema>

Use of the Altova User Forum(s) is governed by the Altova Terms of Use.