Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中读取和修改HTML文件_Java_Html_Arrays_String_File - Fatal编程技术网

在Java中读取和修改HTML文件

在Java中读取和修改HTML文件,java,html,arrays,string,file,Java,Html,Arrays,String,File,我有一个HTML文件,我要做的是将这个文件存储到一些数组或大字符串中,然后进行必要的修改。我必须包括一些Javascript和一些其他带有属性的元素,并且还必须消除其中的一些元素。有人能帮我做这件事吗。谢谢你 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD

我有一个HTML文件,我要做的是将这个文件存储到一些数组或大字符串中,然后进行必要的修改。我必须包括一些Javascript和一些其他带有属性的元素,并且还必须消除其中的一些元素。有人能帮我做这件事吗。谢谢你

<?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> 

eXe
@导入url(base.css);
@导入url(content.css);

第一部分 真假问题


1>SQL代表结构查询语言?
正确 假的 对的 不对!


您可以使用该方法读取html(转换为
字符串
)。在这之后,您的任务实际上是执行一些
String
拆分、替换和插入,最后,在您从硬盘读取文件时使用类似的方法将其全部写入硬盘上的
html
文件。

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或类似的工具。如果没有,请查看

@Genzer Hi,我曾尝试使用BufferedReader读取该文件,但不知道如何向其中添加所需的元素。我试着看了这个例子——非常感谢你的建议。我现在就这样试试。