2013-11-01 54 views
0

所以,我有一个XHTML文档报告框架,我想通过获取某些ID的元素并设置其内容来填充它。Java getElementById()或Alternative

我试过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。所以在希望支持的XHTML文档中尝试使用xml:id而不是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> 
+0

你应该提到的是你想率先实现什么? – Makky

+0

@Makky我想要一个具有特定ID的元素,然后设置它的内容。如果不明确,道歉。 –

+0

发布您的HTML文件 – Makky

回答

0

我不知道为什么它不工作,但我已经为你组装了一个例子,它的工作原理!

注:我的例子是使用以下库

  1. 阿帕奇百科全书IO(Link
  2. Jsoup HTML解析器(Jsoup link)
  3. 阿帕奇百科全书郎(Link

我的输入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> 
+0

谢谢你深思熟虑的答案。我已经接受了它,但我实际上使用了Java标准的XML DOM解析器,这已经完成了。 –

+0

不客气。 – Makky