Issue
I know there are some similar threads out there but none of them are any help to me, I'm trying to create XML file via Java but I'm seeing this error constantly on this line
doc = db.parse(is);
, can someone please tell me where am I going wrong ?
Error:-
Severe: [Fatal Error] :2:2: The markup in the document following the root element must be well-formed.
Severe: org.xml.sax.SAXParseException; lineNumber: 2; columnNumber: 2; The markup in the document following the root element must be well-formed.
at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:257)
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:348)
at com.digivote.digivote.XmlServlet.doPost(XmlServlet.java:194)
My code:
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
String level = request.getParameter("level");
String add = request.getParameter("add");
String addCat = request.getParameter("addCat");
String addQuestions = request.getParameter("addQuestion");
String add_multiChoice = request.getParameter("add_multiChoice");
try
{
DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
// root elements
Document doc = db.newDocument();
Element rootElement = doc.createElement("XML");
doc.appendChild(rootElement);
/***** GeneratedSet ****/
Element genSet = doc.createElement("GeneratedSet");
rootElement.appendChild(genSet);
Attr genDate = doc.createAttribute("genDate");
genDate.setValue("11/01/2018");
genSet.setAttributeNode(genDate);
Attr genBy = doc.createAttribute("genBy");
genBy.setValue("Tom Jones");
genSet.setAttributeNode(genBy);
Attr lev = doc.createAttribute("level");
lev.setValue(level);
genSet.setAttributeNode(lev);
/****** Questions *****/
Element questions = doc.createElement("Questions");
genSet.appendChild(questions);
Attr noq = doc.createAttribute("Questions");
noq.setValue(addQuestions);
questions.setAttributeNode(noq);
Attr type = doc.createAttribute("Type");
type.setValue(add);
questions.setAttributeNode(type);
/****** Question *****/
int numOfQues = Integer.parseInt(addQuestions) ;
int leve = Integer.parseInt(level) ;
String retXML = null;
if(add != null){
InputSource is = new InputSource();
genMaths gm = new genMaths();
retXML = gm.additionQ(leve, 2, numOfQues, 0);
is.setCharacterStream(new StringReader(retXML));
doc = db.parse(is);
//doc = db.parse(new InputSource(new StringReader(is.toString())));
}
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File("C:\\Users\\xxxx\\Documents\\XML Files\\RandomQuestions.xml"));
transformer.transform(source, result);
System.out.println("File saved!");
} catch (ParserConfigurationException | TransformerException pce) {} catch (SAXException ex) {
Logger.getLogger(XmlServlet.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
retXML:-
public String forwardQ ( int level, int operands, int operation, int questions, int type )
{
StringBuffer retXML = new StringBuffer ();
float operand = 0;
float answer = 0;
int minVal = (int)getLevelMin(level);
int maxVal = (int)getLevelMax(level);
String addSign = "";
for ( int j = 0; j < questions; j++ )
{
retXML.append("<Question Text='");
addSign = "";
if ( operation == 1 )
{
answer = 0;
}
else
{
answer = 1;
}
for ( int i = 0; i < operands; i++ )
{
operand = randNum(minVal, maxVal);
if ( operation == 1 )
{
answer = answer + operand;
}
else
{
answer = answer * operand;
}
retXML.append( addSign + operand );
if ( operation == 1 )
{
addSign = " + ";
}
else
{
addSign = " x ";
}
}
if ( type == 1 )
{
retXML.append("' Answer='"+ answer + "'>");
}
else
{
retXML.append(createMultiChoice(answer));
}
retXML.append("</Question>" + System.getProperty("line.separator"));
}
return ( retXML.toString() );
}
public String additionQ ( int level, int operands, int questions, int type )
{
return ( forwardQ ( level, operands, 1, questions, type ) );
}
Solution
From your code, it looks like your XML will end up as
<Question ...></Question>
<Question ...></Question>
You cannot have multiple root elements in an XML document. You could try wrapping all your Question elements in a root element, like
<Questions>
<Question ...></Question>
<Question ...></Question>
</Questions>
Answered By - twinklehawk
Answer Checked By - Cary Denson (JavaFixing Admin)