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

  • Home
  • SEARCH
  • 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 6241815
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T11:50:11+00:00 2026-05-24T11:50:11+00:00

I have a sample code in which i am trying to validate a xml

  • 0

I have a sample code in which i am trying to validate a xml using xml validation method.
And it works correctly too except for minOccurs.I have given the code below. Please help me to find my mistake.

XSD File (Live.xsd):-

            <?xml version="1.0" encoding="UTF-8"?>

             <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">

                    <xsd:element name="Test" />

                    <xsd:complexType name="Test">
                        <xsd:sequence>
                        <xsd:element ref="player" />
                    </xsd:sequence> 
                    </xsd:complexType>


                    <xsd:element name="player" >

                    <xsd:complexType>
                        <xsd:sequence minOccurs="2">
                          <xsd:element name="name" type="xsd:string"/>
                          <xsd:element name="address">
                                  <xsd:complexType>
                                    <xsd:sequence>
                                      <xsd:element name="houseno" type="xsd:int" />
                                      <xsd:element name="street" type="xsd:string" />
                                    </xsd:sequence>
                                  </xsd:complexType>
                                </xsd:element>
                        </xsd:sequence>
                        <xsd:attribute name="id" type="xsd:string" />
                      </xsd:complexType>    
                      </xsd:element>


            </xsd:schema>

Xml file (example.xml) :-

                <?xml version="1.0" encoding="UTF-8"?>

                <Test xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="Live.xsd" >

                    <player id="1">
                        <name>Owen</name>
                        <address>
                            <houseno>10</houseno>
                            <street>downing hill</street>       
                        </address>
                    </player>

                </Test> 

Java method :-

                                 private void validate(File xml) {
                        try {
                            url = new URL(xsd.toURI().toString());//xsd
                        } catch (MalformedURLException e) {
                            e.printStackTrace();
                        }


                        source = new StreamSource(xml); //xml
                        try {
                            System.out.println(url);
                            schema = schemaFactory.newSchema(url);
                        } catch (SAXException e) {
                            e.printStackTrace();
                        }
                        validator = schema.newValidator();
                        System.out.println(xml);
                        try {
                            validator.validate(source);
                        } catch (SAXException e) {
                            e.printStackTrace();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }

I tried giving minOccurs=2 inside < xsd element ref=”player” /> too but didint work

  • 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-24T11:50:12+00:00Added an answer on May 24, 2026 at 11:50 am

    Remember that the default value for maxOccurs is 1. You’re allowing a minimum of 2 and a maximum of 1 …

    Try <xsd:sequence minOccurs="2" maxOccurs="unbounded">

    Edit: careful with ‘unbounded’ though; you’d want to set it to an acceptable maximum without giving anyone the chance of bombarding your system with a gazillion of nodes.

    Edit2: provided the xsd (corrected with maxOccurs) and xml above, this code will output “Validation failed!!!”:

    import java.io.File;
    import java.io.IOException;
    import java.net.MalformedURLException;
    import java.net.URL;
    
    import javax.xml.XMLConstants;
    import javax.xml.transform.stream.StreamSource;
    import javax.xml.validation.Schema;
    import javax.xml.validation.SchemaFactory;
    import javax.xml.validation.Validator;
    
    import org.xml.sax.SAXException;
    import org.xml.sax.SAXParseException;
    
    public class XMLValidate {
    
    static File xsd;
    static File xml;
    static URL url;
    static StreamSource source;
    static SchemaFactory schemaFactory;
    static Schema schema;
    static Validator validator;
    
    /**
     * @param args
     */
    public static void main(String[] args) {
        xml = new File("example.xml");
        xsd = new File("Live.xsd");
    
        try {
            url = new URL(xsd.toURI().toString());// xsd
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    
        source = new StreamSource(xml); // xml
        try {
            //System.out.println(url);
            schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
            schema = schemaFactory.newSchema(url);
        } catch (SAXException e) {
            e.printStackTrace();
        }
        validator = schema.newValidator();
        //System.out.println(xml);
        try {
            validator.validate(source);
            System.out.println("Validation succesful!");
        } catch (SAXParseException e) {
            System.out.println("Validation failed!!!");
            //e.printStackTrace(); -- uncomment for detailed info on validation failing
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    }
    

    If you change the code to validate the following example2.xml, it will output “Validation succesful!”:

    <?xml version="1.0" encoding="UTF-8"?>
    
    <Test xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    
    <player id="1">
        <name>Owen</name>
        <address>
            <houseno>10</houseno>
            <street>downing hill</street>
        </address>
        <name>Maggy</name>
        <address>
            <houseno>10</houseno>
            <street>downing hill</street>
        </address>
    </player>
    
    </Test>
    

    Your xsd with the minOccurs=2 forces you to have the sequence name-address twice in the xml. Not sure if that’s what you were looking for in the first place; it strikes me as odd but I don’t have insight in the requirements.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following sample code which doesn't seem to want to run. import
Does anyone have sample code, or a tutorial which demonstrates how to use Grappa
hi i forgot the code which in a sample class you have to add
Does anyone have some sample code showing how to POST to a URL using
I'm trying to validate an XDocument with a compiled schema that I have (which
I have downloaded TableView sample code '4_TableViewCellSubviews' from internet. I am trying to run
I have some C# code which is using CSharpCodeProvider.CompileAssemblyFromSource to create an assembly in
I currently have method which is trying to find out what the obj is
I have the following sample code which gets a list of values from a
I'm using the following code (which is a sample from the MSDN slightly modified)

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.