Link to home
Start Free TrialLog in
Avatar of AjooAli
AjooAli

asked on

How to check If XML string is well-Formed?

How do I check if the XML string is well formed. something wrong with code. Can someone help me on this please
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
      
                        response.setContentType("text/plain");
        PrintWriter out = response.getWriter();

        HttpSession session = request.getSession();

        String xml = request.getParameter("xmldoc");
        checkIfXMLIsWellFormed(xml);
        //out.println(xml);
      
      }

public void checkIfXMLIsWellFormed(String aXml){
            
            try{
                  
                  File file = new File(aXml);
                  if(file.exists()){
                        
                        XMLReader reader = XMLReaderFactory.createXMLReader();
                        reader.parse(aXml);
                        
                  } else {
                        
                        System.out.println("File not Found");
                  }
                  
                  
                  
                  
            } catch(SAXException e){
                  
                        System.out.println( aXml + "Is not well-formed");
                  
            }
            catch(IOException io){
                  
                  System.out.println(io.getMessage());
            }
            
      }
Avatar of Tomas Helgi Johannsson
Tomas Helgi Johannsson
Flag of Iceland image

              Hi!

You could use this example to see where in the xml file the error is
http://www.java2s.com/Code/Java/XML/HandlingSAXerrorsduringparsing.htm

Also take a look the API : http://java.sun.com/j2se/1.5.0/docs/api/org/xml/sax/SAXParseException.html
for more info.

You can using the SAXParseException where in the XML file the parsing exception occurs.

Regards,
     Tomas Helgi
Avatar of AjooAli
AjooAli

ASKER

Ok, what I want to do is to parse an xml string not the xml file. I recieve this xml string from another servelt.
Avatar of AjooAli

ASKER

Here is code and exception is get:
C:\Java_J2ee\Downloads\eclipse-jee-galileo-SR1-win32\eclipse\<?xml version="1.0" encoding="ISO-8859-1"?><note>  <from>Jani<\from>  <to>Tove<\to>  <message>Send me Amount details<\message>    <\note>
 (The filename, directory name, or volume label syntax is incorrect)

// TODO Auto-generated method stub
      
            System.out.println("Inside CSMServlet");
            response.setContentType("text/plain");
        PrintWriter out = response.getWriter();

        HttpSession session = request.getSession();

        String xml = request.getParameter("xmldoc");
        checkIfXMLIsWellFormed(xml);
        //out.println(xml);
      
      }
      
      
      public void checkIfXMLIsWellFormed(String aXml){
            
            try{
                  
                        DocumentBuilderFactory dBF = DocumentBuilderFactory.newInstance();
                        DocumentBuilder builder = dBF.newDocumentBuilder();
                        InputSource is = new InputSource(aXml);
                    Document doc = builder.parse(is);
                   
                    System.out.println(aXml + " is well-formed!");
                     
                     
                  
                  
            } catch(ParserConfigurationException e){
                  
                        System.out.println( aXml + "Is not well-formed");
                  
            }
            catch(SAXException se){
                  
                  System.out.println(se.getMessage());
            }
            catch(IOException io){
                  
                  System.out.println(io.getMessage());
            }
            
      }
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
        Hi!

You could use the SAXException also to output where in the XML string the error is

  catch(SAXException se){
                 
                  System.out.println(se.getMessage());
                  System.out.println(se.getLineNumber());
                  System.out.println(se.getColumnNumber());
                  System.out.println(se.getPublicId() );
                  System.out.println(se.getSystemId() );
            }

Hope this helps.

Regards,
    Tomas Helgi
Avatar of AjooAli

ASKER

Great