简体   繁体   English

使用SAX和W3C Java库的XML / HTML构建器类无法处理HTML输入

[英]XML/HTML builder class using SAX and W3C Java libraries can't handle HTML input

All, 所有,

I am developing a Java Web service that fetches archived data from an external DB. 我正在开发一个Java Web服务,该服务可以从外部数据库中获取存档的数据。

The requirement is that as well as returning the results as an XML message the client can also request to have the results presented on a HTML page. 要求是,除了将结果作为XML消息返回之外,客户端还可以请求将结果显示在HTML页面上。

This diagram describes the high level design: 下图描述了高级设计:

在此处输入图片说明

I have realised though while doing my implementation that XML and HTML are not exactly the same eg: 尽管在实现过程中我已经意识到XML和HTML并不完全相同,例如:

  • HTML parsing can tolerate specific elements not being closed. HTML解析可以容忍未关闭的特定元素。

This causes my current class implementation to throw up these errors (caused by the HTML input) 这导致我当前的类实现抛出这些错误(由HTML输入引起)

Error Messages 错误讯息

[03/Mar/2014:09:12:05] warning (19052):     CORE3283: stderr: [Fatal Error] web.html:1:3: The markup in the document preceding the root element must be well-formed.
[03/Mar/2014:09:12:05] warning (19052):     CORE3283: stderr: org.xml.sax.SAXParseException: The markup in the document preceding the root element must be well-formed. 

My Code 我的密码

    import org.w3c.dom.*;
import javax.xml.parsers.*;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;


public class OutputBuilder
{
    private DocumentBuilderFactory docBF;
    private DocumentBuilder docBuilder;
    private Document doc;
    private static float UUID;
    private String docType;

    public OutputBuilder(String template, String output) throws ParserConfigurationException, SAXException, IOException
    {
        docBF = DocumentBuilderFactory.newInstance();
        docBuilder = docBF.newDocumentBuilder();

        //set the base document to the specified template file
        doc = docBuilder.parse(new File(template));
        //
        docType = output;
    }

    /*
     * Build the final document by adding values passed in from query results
     */
    public void fillTemplate(ResultSet qR) throws SQLException
    {
        if(docType.equals("html"))
        {
            //find the designated point of data insertion to the html document
            Element appendPoint = doc.getElementById("archive_table");

            //get meta data column names for table header row
            ResultSetMetaData rsmd = qR.getMetaData();

            //generate this first row which is the header
            Element headerRow = doc.createElement("tr");

            //create a column in the table header for each column in the query results
            for (int i = 0; i < rsmd.getColumnCount(); i++)
            {
                Element tableH = doc.createElement("th");
                tableH.setNodeValue(rsmd.getColumnName(i));
                headerRow.appendChild(tableH);              
            }

            //append header row to table
            appendPoint.appendChild(headerRow);

            //fill table body rows with query results

            while(qR.next())
            {
                //create a table row for each row in query results
                Element bodyRow = doc.createElement("tr"); 


                //fill that row with all column values in query results
                for(int i = 0; i < rsmd.getColumnCount(); i++)
                {
                    Element tableB = doc.createElement("td");
                    tableB.setNodeValue(qR.getString(i));
                    bodyRow.appendChild(tableB);
                }
                //add each constructed row to the table
                appendPoint.appendChild(bodyRow);
            }

        }
        else
        {
            //do XML construction
        }

    }

}

What specific libraries or new logic must I do to allow my class to handle both XML and HTML construction? 我必须做什么特定的库或新逻辑,以允许我的类处理XML和HTML构造?

Any other suggestions welcome! 任何其他建议,欢迎您!

Ps upvote for a fine specimen of a question PS赞成一个问题的好标本

I suggest that you start using dedicated html parser for such opertations. 我建议您开始使用专用的html解析器进行此类操作。 I personally use Jsoup You can build own HTML structure using it as well. 我个人使用Jsoup。您也可以使用它构建自己的HTML结构。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM