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.

Raptor Java API Example Options · View
charpel
Posted: Friday, March 18, 2016 3:38:28 PM
Rank: Newbie

Joined: 3/18/2016
Posts: 1
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>

Users browsing this topic
guest

Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

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