本文介绍了org.xml.sax.SAXParseException 文档中根元素后面的标记必须格式正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道那里有一些类似的线程,但没有一个对我有任何帮助,我正在尝试通过 Java 创建 XML 文件,但我在这条线上不断看到此错误doc = db.parse(is);
,有人可以告诉我我哪里错了吗?错误:-
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 linedoc = 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)
我的代码:
@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 ) );
}
推荐答案
从你的代码来看,你的 XML 看起来像
From your code, it looks like your XML will end up as
<Question ...></Question>
<Question ...></Question>
一个 XML 文档中不能有多个根元素.您可以尝试将所有问题元素包装在根元素中,例如
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>
这篇关于org.xml.sax.SAXParseException 文档中根元素后面的标记必须格式正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!