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());
            }
            
      }
AjooAliAsked:
Who is Participating?
 
Tomas Helgi JohannssonCommented:
              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
0
 
AjooAliAuthor Commented:
Ok, what I want to do is to parse an xml string not the xml file. I recieve this xml string from another servelt.
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
AjooAliAuthor Commented:
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());
            }
            
      }
0
 
Tomas Helgi JohannssonCommented:
        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
0
 
AjooAliAuthor Commented:
Great
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.