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("t ext/plain" );
PrintWriter out = response.getWriter();
HttpSession session = request.getSession();
String xml = request.getParameter("xmld oc");
checkIfXMLIsWellFormed(xml );
//out.println(xml);
}
public void checkIfXMLIsWellFormed(Str ing aXml){
try{
File file = new File(aXml);
if(file.exists()){
XMLReader reader = XMLReaderFactory.createXML Reader();
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.getM essage());
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("t
PrintWriter out = response.getWriter();
HttpSession session = request.getSession();
String xml = request.getParameter("xmld
checkIfXMLIsWellFormed(xml
//out.println(xml);
}
public void checkIfXMLIsWellFormed(Str
try{
File file = new File(aXml);
if(file.exists()){
XMLReader reader = XMLReaderFactory.createXML
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.getM
}
}
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.
ASKER
Here is code and exception is get:
C:\Java_J2ee\Downloads\ecl ipse-jee-g alileo-SR1 -win32\ecl ipse\<?xml version="1.0" encoding="ISO-8859-1"?><no te> <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("t ext/plain" );
PrintWriter out = response.getWriter();
HttpSession session = request.getSession();
String xml = request.getParameter("xmld oc");
checkIfXMLIsWellFormed(xml );
//out.println(xml);
}
public void checkIfXMLIsWellFormed(Str ing aXml){
try{
DocumentBuilderFactory dBF = DocumentBuilderFactory.new Instance() ;
DocumentBuilder builder = dBF.newDocumentBuilder();
InputSource is = new InputSource(aXml);
Document doc = builder.parse(is);
System.out.println(aXml + " is well-formed!");
} catch(ParserConfigurationE xception e){
System.out.println( aXml + "Is not well-formed");
}
catch(SAXException se){
System.out.println(se.getM essage());
}
catch(IOException io){
System.out.println(io.getM essage());
}
}
C:\Java_J2ee\Downloads\ecl
(The filename, directory name, or volume label syntax is incorrect)
// TODO Auto-generated method stub
System.out.println("Inside
response.setContentType("t
PrintWriter out = response.getWriter();
HttpSession session = request.getSession();
String xml = request.getParameter("xmld
checkIfXMLIsWellFormed(xml
//out.println(xml);
}
public void checkIfXMLIsWellFormed(Str
try{
DocumentBuilderFactory dBF = DocumentBuilderFactory.new
DocumentBuilder builder = dBF.newDocumentBuilder();
InputSource is = new InputSource(aXml);
Document doc = builder.parse(is);
System.out.println(aXml + " is well-formed!");
} catch(ParserConfigurationE
System.out.println( aXml + "Is not well-formed");
}
catch(SAXException se){
System.out.println(se.getM
}
catch(IOException io){
System.out.println(io.getM
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hi!
You could use the SAXException also to output where in the XML string the error is
catch(SAXException se){
System.out.println(se.getM essage());
System.out.println(se.getL ineNumber( ));
System.out.println(se.getC olumnNumbe r());
System.out.println(se.getP ublicId() );
System.out.println(se.getS ystemId() );
}
Hope this helps.
Regards,
Tomas Helgi
You could use the SAXException also to output where in the XML string the error is
catch(SAXException se){
System.out.println(se.getM
System.out.println(se.getL
System.out.println(se.getC
System.out.println(se.getP
System.out.println(se.getS
}
Hope this helps.
Regards,
Tomas Helgi
ASKER
Great
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