We help IT Professionals succeed at work.
Get Started
Troubleshooting Question

How do I generalise a method to receive variable number of parameter values and further process it?

sukhoi35
sukhoi35 asked
on
55 Views
Last Modified: 2020-08-15
Hi Experts,
After referring to online samples, I have been successful in building a method which accepts a single XML and XSD as input for schema validation. However, I need to generalise this method to accept a dynamic number of XSD paths, define the StreamSources for every path and use them all in factory.newSchema method/constructor. Can I please get suggestions on how this can be best achieved? I was thinking of passing a string list as a parameter, however, not sure how I can use them to define Streamsrouce iteratively and further use them all in factory.newschema. Thanks.
   public static boolean validateXMLSchema(String xsdPath, String xmlData){
        
        try {        
            
            SchemaFactory factory =
                    SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);            
            StreamSource mainXSD = new StreamSource(new File("C:\\XSDs\\main.xsd"));            

            Schema schema = factory.newSchema(new Source[]{mainXSD});

            Validator validator = schema.newValidator();
            validator.validate(new StreamSource(new StringReader(xmlData)));
        } catch (SAXException | IOException e) {
            System.out.println("Exception: "+e.getMessage());
            return false;
        }
        return true;
    }



Comment
Watch Question
Senior Developer
CERTIFIED EXPERT
Commented:
This problem has been solved!
Unlock 1 Answer and 9 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant

An Experts Exchange subscription includes unlimited access to online courses.

Get Started
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE