0

所以,我有一个 XHTML 文档报告框架,我想通过获取特定 ID 的元素并设置它们的内容来填充它。

我尝试了 getElementById(),并返回了 null(因为我发现,id 不是隐含的“id”,需要在模式中声明)。

panel.setDocument(Main.class.getResource("/halreportview/DefaultSiteDetails.html").toString());
panel = populateDefaultReport(panel);

Element header1 = panel.getDocument().getElementById("header1");
header1.setTextContent("<span class=\"b\">Instruction Type:</span> Example<br/><span class=\"b\">Allocated To:</span> "+employee.toString()+"<br/><span class=\"b\">Scheduled Date:</span> "+dateFormat.format(scheduledDate));

因此,我尝试了一些变通方法,因为我不想验证我的 XHTML 文档。我尝试像这样在有问题的文件顶部添加一个快速 DTD;

<?xml version="1.0"?>
<!DOCTYPE foo [<!ATTLIST bar id ID #IMPLIED>]>

但是 getElementById() 仍然返回 null。因此尝试xml:id在 XHTML 文档中使用而不是 id 以希望它得到支持,但还是没有运气。因此,我尝试使用 getElementsByTagName() 并遍历结果检查 ID。这有效,并找到了正确的元素(通过输出打印“找到它”确认),但是当我尝试在这个元素上调用 setTextContent 时,我仍然收到 NullPointException。代码如下;

    Element header1;
    NodeList sections = panel.getDocument().getElementsByTagName("p");
    for (int i = 0; i < sections.getLength(); ++i) {
         if (((Element)sections.item(i)).getAttribute("id").equals("header1")) {
            System.out.println("Found it");
            header1 = (Element) sections.item(i);
            header1.setTextContent("<span class=\"b\">Instruction Type:</span> Example<br/><span class=\"b\">Allocated To:</span> "+employee.toString()+"<br/><span class=\"b\">Scheduled Date:</span> "+dateFormat.format(scheduledDate));
        }
    }

我对这个失去了理智。我一定对这应该如何工作存在某种根本性的误解。有任何想法吗?

编辑; 下面是我的 XHTML 文件的摘录,去掉了 CSS。

<html>
<head>
    <title>Site Details</title>
    <style type="text/css">
    </style>
</head>
<body>
    <div class="header">
        <p></p>
        <img src="#" alt="Logo" height="81" width="69"/>
        <p id="header1"><span class="b">Instruction Type:</span> Example<br/><span class="b">Allocated To:</span> Example<br/><span class="b">Scheduled Date:</span> Example</p> 
    </div>
</body>
</html>
4

1 回答 1

0

我不知道为什么它不工作,但我已经为你整理了一个例子,它可以工作!

注意:我的示例使用以下库

  1. Apache Commons IO(链接
  2. Jsoup HTML 解析器(Jsoup 链接)
  3. Apache Commons Lang(链接

我的输入 xhtml 文件,

<html>
<head>
<title>Site Details</title>
<style type="text/css">
</style>
</head>
<body>
    <div class="header">
        <p></p>
        <img src="#" alt="Logo" height="81" width="69" />
        <p id="header1">
            <span class="b">Instruction Type:</span> Example<br />
            <span class="b">Allocated To:</span> Example<br />
            <span class="b">Scheduled Date:</span> Example
        </p>
    </div>
</body>
</html>

有效的java代码![所有评论,阅读]

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringEscapeUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class Test {

    /**
     * @param args
     * @throws IOException
     * @throws InterruptedException
     */
    public static void main(String[] args) throws IOException, InterruptedException {
        //loading file from project
        //When it is exported as JAR the files inside jar are not files but they are stream
        InputStream stream = Test.class.getResourceAsStream("/test.xhtml");

        //convert stream to file
        File xhtmlfile = File.createTempFile("xhtmlFile", ".tmp");
        FileOutputStream fileOutputStream = new FileOutputStream(xhtmlfile);
        IOUtils.copy(stream, fileOutputStream);
        xhtmlfile.deleteOnExit();

        //get html string from file
        String htmlString = FileUtils.readFileToString(xhtmlfile);

        //parse using jsoup
        Document doc = Jsoup.parse(htmlString);

        //get all elements
        Elements allElements = doc.getAllElements();


        for (Element el : allElements) {
            //if element id is header 1
            if (el.id().equals("header1")) {

                //dummy emp name
                String employeeName = "dummyEmployee";
                //update text
                el.text("<span class=\"b\">Instruction Type:</span> Example<br/><span class=\"b\">Allocated To:</span> "
                        + employeeName.toString() + "<br/><span class=\"b\">Scheduled Date:</span> " + new Date());
                //dont loop further
                break;
            }
        }

        //now get html from the updated document
        String html = doc.html();

        //we need to unscape html 
        String escapeHtml4 = StringEscapeUtils.unescapeHtml4(html);

        //print html 
        System.out.println(escapeHtml4);

    }

}

*输出 *

<html>
 <head> 
  <title>Site Details</title> 
  <style type="text/css">
</style> 
 </head> 
 <body> 
  <div class="header"> 
   <p></p> 
   <img src="#" alt="Logo" height="81" width="69" /> 
   <p id="header1"><span class="b">Instruction Type:</span> Example<br/><span class="b">Allocated To:</span> dummyEmployee<br/><span class="b">Scheduled Date:</span> Sat Nov 02 07:37:12 GMT 2013</p> 
  </div>  
 </body>
</html>
于 2013-11-02T07:38:13.910 回答