Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7193373
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:09:27+00:00 2026-05-28T20:09:27+00:00

I have problems with validating the xml file against xml schema in case when

  • 0

I have problems with validating the xml file against xml schema in case when xml document contains schema. The xml file looks like:

<?xml version="1.0"?>
<catalog xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
         xmlns:x="urn:book"> 
<!-- START OF SCHEMA -->
<xsd:schema targetNamespace="urn:book">
 <xsd:element name="book">
  <xsd:complexType>
    <xsd:sequence>
      <xsd:element name="author" type="xsd:string"/>
      <xsd:element name="title" type="xsd:string"/>
      <xsd:element name="genre" type="xsd:string"/>
      <xsd:element name="price" type="xsd:float"/>
      <xsd:element name="publish_date" type="xsd:date"/>
      <xsd:element name="description" type="xsd:string"/>
    </xsd:sequence>
    <xsd:attribute name="id" type="xsd:string"/>
  </xsd:complexType>
 </xsd:element>
</xsd:schema>
<!-- END OF SCHEMA -->
   <x:book id="bk101">
      <author>Gambardella, Matthew</author>
      <title>XML Developer's Guide</title>
      <genre>Computer</genre>
      <price>44.95</price>
      <publish_date>2000-10-01</publish_date>
      <description>An in-depth look at creating applications with
      XML.</description>
   </x:book>
</catalog>

java code looks like:

// define the type of schema - we use W3C:
String schemaLang = "http://www.w3.org/2001/XMLSchema";

// get validation driver:
SchemaFactory factory = SchemaFactory.newInstance(schemaLang);

// create schema by reading it from an XSD file:
Schema schema = factory.newSchema(new StreamSource("..........."));
Validator validator = schema.newValidator();

// at last perform validation:
validator.validate(new StreamSource("myDoc.xml"));

And the problem for me is how to use SchemaFactory object in this case ?

I’m greatful for any help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T20:09:28+00:00Added an answer on May 28, 2026 at 8:09 pm

    I assume this is what you want; the code is meant to illustrate, rather than account for good programming practices. It was tested with your XML. The main assumption is that the document element has two elements, first one the XSD, second the XML to validate.

    If, for example, you change 44.95 to d44.95 you will get this output:

    XML is not valid because cvc-datatype-valid.1.2.1: ‘d44.95’ is not a valid value for ‘float’.

    Otherwise, everything goes fine and the program prints XML is valid.

    import java.io.*;
    import javax.xml.parsers.ParserConfigurationException;
    import javax.xml.transform.Source;
    import javax.xml.transform.dom.DOMSource;
    import javax.xml.validation.*;
    import org.w3c.dom.NodeList;
    import org.w3c.dom.Node;
    import org.xml.sax.SAXException;
    import javax.xml.xpath.*;
    import org.xml.sax.InputSource;
    
    public class TestValidation {
        public static void main(String[] args) throws SAXException, IOException, ParserConfigurationException, XPathExpressionException {
            XPath xpath = XPathFactory.newInstance().newXPath();
            NodeList nodes = (NodeList)xpath.evaluate("/*/*", new InputSource("XmlWithEmbeddedXsd.xml"), XPathConstants.NODESET);
            SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
            Validator validator = factory.newSchema(new DOMSource(nodes.item(0))).newValidator();
            try {
                validator.validate(new DOMSource(nodes.item(1)));
                System.out.println("XML is valid.");
            }
            catch (SAXException ex) {
                System.out.println("XML is not valid because " + ex.getMessage());
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an XML document that looks like this: <?xml version=1.0 encoding=UTF-8?> <xs:msgdata xmlns:xs=http://www.myCompany.com
I need to validate a XML file against a schema. The XML file is
I have an XML-like file: <p>something</p> <ac:image> <ri:attachment ri:filename=IMAGE.PNG /> </ac:image> <ac:macro ac:name=screenshot> <ac:default-parameter>IMAGE.ss</ac:default-parameter>
I have problems to get rspec running properly to test validates_inclusion_of my migration looks
Someone else has already asked a somewhat similar question: Validate an Xml file against
I have a problem with XML validation with expat. I have not found document
I would like to use Gwt-Validation with Gwt 2.4.0. I have in my gwt.xml
i have xsd and xml file. xml parse fine when validation is turned off.
I have a large XML file (~18MB). Apparently there is a tag somewhere in
I have a process that is parsing an XML file. This is occuring in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.