我在JS中有以下功能:
function replaceSrcc() {
var elem = document.getElementsByTagName("embed")[0];
copy = elem.cloneNode();
copy.src = DataVideo;
elem.parentNode.replaceChild(copy, elem);
}
我正在尝试使用Java中的HTML解析器将关键字DataVideo
替换为其他值videoID
:
if(tag instanceof ScriptTag){
if("DataVideo".equals(tag.getAttribute("copy.src"))){
tag.setAttribute("copy.src", videoID);
}
};
然而,这不起作用。我假设我在copy.src
标签中找到<script>
元素时出错,我认为它不是属性,但我不确切知道如何找到它。
编辑:负责分析HTML的整个班级MyNodeVisitor看起来像这样:
public class MyNodeVisitor extends NodeVisitor {
String name;
String text;
String storyText;
String videoID;
ArrayList<String> pictures;
public MyNodeVisitor() {
}
public MyNodeVisitor(ArrayList<String> Pictures, String Name, String Text, String StoryText, String VideoId){
this.name = Name;
this.text = Text;
this.storyText = StoryText;
this.videoID = VideoId;
this.pictures = Pictures;
}
public void visitStringNode (Text string)
{
if (string.getText().equals("DataName")) {
string.setText(name);
}
else if (string.getText().equals("DataText")){
string.setText(text);
}
else if (string.getText().equals("DataStory")){
string.setText(storyText);
}
}
@Override
public void visitTag (Tag tag)
{
if(tag instanceof ImageTag){
if("DataIcon".equals(tag.getAttribute("src"))){
tag.setAttribute("src", pictures.get(0));
}
else if("DataIcon2".equals(tag.getAttribute("src"))){
tag.setAttribute("src", pictures.get(1));
}
else if("DataIcon3".equals(tag.getAttribute("src"))){
tag.setAttribute("src", pictures.get(2));
}
else if("DataIcon4".equals(tag.getAttribute("src"))){
tag.setAttribute("src", pictures.get(3));
}
else if("DataIcon5".equals(tag.getAttribute("src"))){
tag.setAttribute("src", pictures.get(4));
}
}
else if(tag instanceof ScriptTag){
if(((ScriptTag) tag).getStringText().contains("DataVideo")){
String tagText = ((ScriptTag)tag).getStringText();
//String tagText = tagText.replaceAll("DataVideo",videoID);
}
/*NodeList nodes = ((ScriptTag)tag).getChildren();
for (int i = 0; i < nodes.size(); i++){
if (nodes.elementAt(i).getText().equals("DataVideo")){
nodes.elementAt(i).setText(videoID);*/
}
}
}
编辑: 这是预先准备好的HTML模板:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang="en">
<head>
....
</head>
<body>
<script>
function replaceSrcc() {
var elem = document.getElementsByTagName("embed")[0];
copy = elem.cloneNode();
copy.src = DataVideo;
elem.parentNode.replaceChild(copy, elem);
}
if (window.addEventListener){
window.addEventListener('load', replaceSrcc, false);
} else if (window.attachEvent) {
window.attachEvent('onload', replaceSrcc);
}
</script>
<h1 align="center">DataName</h1>
<div class="tabber">
<div class="tabbertab" bgcolor="blue">
<h2 color="red">Info </h2>
<p>DataText</p>
</div>
<div class="tabbertab">
<h2>Story from TIME</h2>
<p>DataStory</p>
</div>
<div class="tabbertab">
<h2>Pictures</h2>
<div id="album">
<ul class="gallery">
<li><a href="#nogo" tabindex="1">1<img src=DataIcon alt="landscape image 1" title="landscape image 1" /></a></li>
<li><a href="#nogo" tabindex="1">2<img src=DataIcon2 alt="landscape image 2" title="landscape image 2" /></a></li>
<li><a href="#nogo" tabindex="1">3<img src=DataIcon3 alt="portrait image 1" title="portrait image 1" /></a></li>
<li><a href="#nogo" tabindex="1">4<img src=DataIcon4 alt="portrait image 2" title="portrait image 2" /></a></li>
<li><a href="#nogo" tabindex="1">5<img src=DataIcon5 alt="landscape image 3" title="landscape image 3" /></a></li>
</li>
</ul>
</div>
</div>
<div class="tabbertab">
<h2>Video</h2>
<object width="640" height="360">
<param name="movie" value="https://www.youtube.com/v/M7lc1UVf-VE?version=3"></param>
<param name="allowFullScreen" value="true"></param>
<param name="allowScriptAccess" value="always"></param>
<embed src=DataVideo type="application/x-shockwave-flash" allowfullscreen="true" allowScriptAccess="always" width="640" height="360"></embed>
</object>
</div>
</div>
</body>
</html>