• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 7008
  • Last Modified:

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());
            }
            
      }
0
AjooAli
Asked:
AjooAli
  • 3
  • 2
1 Solution
 
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
 
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now