2012-07-14 27 views
3

我有一個HTML文件,我必須做的是將此文件存儲到某個數組或大字符串中,然後執行所需的修改。我必須包含一些Javascript和其他一些具有屬性的元素,並且還必須消除其中的一些元素。任何人都可以幫助我做到這一點。感謝您!在Java中讀取和修改HTML文件

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-type" content="text/html; charset=utf-8" /> 
<title>eXe</title> 
<style type="text/css"> 
@import url(base.css); 
@import url(content.css); 
</style> 
<script type="text/javascript" src="common.js"></script> 
<!--HERE I NEED TO INCLUDE 3 MORE JAVASCRIPTS--> 
</head> 
<body> 
<div id="outer"> 
<div id="main"> 
<div id="nodeDecoration"> 
<p id="nodeTitle"> 
Part 1</p> 
</div> 
<div class="TrueFalseIdevice" id="id12"> 
<script type="text/javascript" src="common.js"></script> 
<!--THIS JAVASCRIPT HAS TO BE ELIMINATED--> 
<script type="text/javascript" src="libot_drag.js"></script> 
<div class="iDevice emphasis1"> 
<img alt="" class="iDevice_icon" src="icon_question.gif" /> 
<span class="iDeviceTitle">True-False Question</span><br/> 
<div class="iDevice_inner"> 
<div id="ta12_16" class="block" style="display:block"> 

</div><div class="question"> 
<br/><br/><div id="taquestion0b12" class="block" style="display:block">1><span style="color: #000000; font-family: Verdana,Arial,Helvetica,sans-serif; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; background-color: #ffffff; display: inline ! important; float: none"> SQL Stands for Structure Query Language?</span> 

<!--THIS ONCLICK EVENT HAS TO BE REMOVED--> 
</div><br/>True <input type="radio" name="option0b12" id="true0b12" onclick="getFeedback(0,2,'0b12','truefalse')"/> 
False <input type="radio" name="option0b12" id="false0b12" onclick="getFeedback(1,2,'0b12','truefalse')"/> 
<div id="s0b0b12" style="color: rgb(0, 51, 204);display: none;" even_steven="18">Correct! </div> 
<div id="s1b0b12" style="color: rgb(0, 51, 204);display: none;" even_steven="19">Incorrect! </div> 
<div id="sfbk0b12" style="color: rgb(0, 51, 204);display: none;"><div id="tafeedback0b12" class="block" style="display:block"> 
<!--HERE I NEED TO INCLUDE A SUBMIT BUTTON--> 
</div></div> 
</div> 
</div> 
</div> 
</body></html> 
+0

到目前爲止,你試過了什麼? – Genzer 2012-07-14 19:03:00

+0

@Genzer嗨,我試圖通過使用BufferedReader讀取文件,但我不知道如何將所需的元素添加到它。我通過看到這個例子試過了 - http://stackoverflow.com/questions/8563294/modifying-existing-file-content-in-java – RahulD 2012-07-14 19:16:38

回答

5

Java已經有一個解析器,稱爲DOM,可以幫助你。你可以使用這樣的事情:

File theXML = new File("C:\\path\\to\\file.xml"); 
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
DocumentBuilder builder = factory.newDocumentBuilder(); 
Document doc = builder.parse(theXML); 
doc.getDocumentElement().normalize(); 

如果你曾經使用JavaScript的DOM,你應該知道現在該做什麼,使用doc.getElementsByTagName等。如果你不知道,退房the oracle tutorial

+0

非常感謝您的建議。我現在就嘗試這種方式。 – RahulD 2012-07-14 19:49:48

0

您可以使用this方式來閱讀你的HTML(成String)。之後,你的任務就是完成一些String分割,替換和插入操作,最後,當你從硬盤讀取數據時,使用類似的方法將它們全部寫入到HDD上的html文件中。

+0

非常感謝答案。 – RahulD 2012-07-14 19:50:15

相關問題