简体   繁体   English

org.xml.sax.SAXParseException 文档中根元素之后的标记必须格式正确

[英]org.xml.sax.SAXParseException The markup in the document following the root element must be well-formed

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);我知道那里有一些类似的线程,但它们对我没有任何帮助,我正在尝试通过 Java 创建 XML 文件,但我在这一行经常看到这个错误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:- 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 ) );
    }

From your code, it looks like your XML will end up as 从您的代码中,您的XML看起来就像最终一样

<Question ...></Question>
<Question ...></Question>

You cannot have multiple root elements in an XML document. XML文档中不能有多个根元素。 You could try wrapping all your Question elements in a root element, like 您可以尝试将所有Question元素包装在根元素中,例如

<Questions>
    <Question ...></Question>
    <Question ...></Question>
</Questions>

Just ensure you only have one element as the root of the contents you are converting只需确保您只有一个元素作为您要转换的内容的根

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 org.xml.sax.SAXParseException:元素的内容必须由格式正确的字符数据或标记组成 - org.xml.sax.SAXParseException: The content of elements must consist of well-formed character data or markup org.xml.sax.SAXParseException日语字符。 元素的内容必须包含格式正确的字符数据或标记 - org.xml.sax.SAXParseException for Japanese character. The content of elements must consist of well-formed character data or markup 错误:根元素后面的文档中的标记必须格式正确 - Error: The markup in the document following the root element must be well-formed 根元素后面的文档中的标记必须格式正确吗? - The markup in the document following the root element must be well-formed? web.xml根元素后面的文档中的标记必须格式正确 - web.xml the markup in the document following the root element must be well-formed Java XML 问题:文档中根元素后面的标记必须格式正确 - Java XML Problem: The markup in the document following the root element must be well-formed org.xml 解析 XML.sax.SAXParseException 时出现异常:元素的内容必须由格式正确的字符数据或标记组成 - org.xmlGetting exception while parsing XML.sax.SAXParseException: The content of elements must consist of well-formed character data or markup Tomcat错误:根元素后的文档中的标记必须格式正确 - Tomcat error: The markup in the document following the root element must be well-formed 添加导航抽屉会导致:根元素后面的文档中的标记必须格式正确 - Adding Navigation Drawer results in: The markup in the document following the root element must be well-formed Android 应用程序,根元素后面的文档中的标记必须格式正确 - Android app, The markup in the document following the root element must be well-formed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM