我正在尝试使用 DOM 在 XHTML 文件的特定位置(在 id="123" 的 div 标记之后)添加一个元素(javascript),我正在使用 getElementById()。因此,为了定位元素的位置,我使用了两种方法。1:不使用 XPath(我在我的代码中评论过) 2:使用 XPath(在论坛中提出建议后)从第一种方法我能够获得相应类的值,其中 id="123" 但它也抛出 NullPointer 异常。所以我使用了 XPath,但是在使用 evaluate(String, Object) 方法时它显示错误。谁能告诉我我需要在这里传递什么参数以及 'sxp' --> NodeList nodes = xPath.evaluate(sxp); 感谢您!
我的 XHTML:
<?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" />
<script></script>
</head>
<body>
<div id="outer">
<div id="main">
<div id="nD">
<p id="nT">XSD</p>
</div>
<div class="TF" id="123">
<!--HERE I NEED TO INCLUDE ONE JAVASCRIPT-->
<div class="iDevice">
<img/>
<span>XYZ</span><br/>
<div>
<div>
</div>
<div class="q">
<br/><br/>
<div>1<span> ABC</span>
</div>
<br/>T <input/> F <input/>
<div></div>
</div></div></div>
</div></div></div>
</body></html>
Java 代码:
public class Dom {
protected static final String XML_FILE_NAME = "C:\\tmp\\part_1.html";
protected static final String XML_FILE_NAME_COPY = "C:\\tmp\\part_1_copy.html";
public static void main(String args[]) throws XPathExpressionException {
try {
File file = new File(XML_FILE_NAME);
//Create instance of DocumentBuilderFactory
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(false);
factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
factory.setNamespaceAware(true);
//Get the DocumentBuilder
DocumentBuilder docBuilder = factory.newDocumentBuilder();
//Using existing XML Document
Document doc = docBuilder.parse(file);
// Get the head element by tag name directly
Node head = doc.getElementsByTagName("head").item(0);
//WITHOUT XPATH
/* NodeList nodelist=doc.getElementsByTagNameNS("http://www.w3.org/1999/xhtml","div");
for (int i=0; i<nodelist.getLength(); i++) {
Element elem=(Element)nodelist.item(i);
if (elem.getAttributeNode("id").getValue().equals("123")) {
//Printing the value for testing
System.out.println(elem.getAttributeNode("class").getValue());
}
} */
//WITH XPATH
String sxp="/x:html/x:body/x:div/x:div/x:div[@id = '123']";
XPath xPath = XPathFactory.newInstance().newXPath();
xPath.setNamespaceContext(new NamespaceContext() {
public String getNamespaceURI(String prefix) {
if (prefix.equals("x")) {
return "http://www.w3.org/1999/xhtml";
}
return XMLConstants.NULL_NS_URI;
}
public String getPrefix(String namespaceURI) {
if (namespaceURI.equals("http://www.w3.org/1999/xhtml")) {
return "xhtml";
}
return null;
}
public Iterator <String> getPrefixes(String namespaceURI) {
return null;
}
});
//Before editing
// NodeList nodes = xPath.evaluate(sxp);
//After Editing but now I get the exception at this line
NodeList nodes=(NodeList)xPath.evaluate(sxp,doc,XPathConstants.NODESET);
for (int i=0; i<nodes.getLength(); i++) {
Element elem=(Element)nodes.item(i);
//each one is the searched div element with id attribute 123
elem.setIdAttribute("id", true);
Element found = doc.getElementById("123");
Element script2 = doc.createElement("script");
script2.setAttribute("src", "cal.js");
script2.setAttribute("type","text/javascript");
script2.setAttribute("xml:space","preserve");
found.appendChild(script2);
}
//set up a transformer
....
.....
}
}
我得到的错误 -
线程“主”javax.xml.transform.TransformerException 中的异常:错误引用的文字...预期的单引号!在 com.sun.org.apache.xpath.internal.compiler.XPathParser.error(Unknown Source) 在 com.sun.org.apache.xpath.internal.compiler.Lexer.tokenize(Unknown Source) 在 com.sun.org .apache.xpath.internal.compiler.Lexer.tokenize(Unknown Source) at com.sun.org.apache.xpath.internal.compiler.XPathParser.initXPath(Unknown Source) at com.sun.org.apache.xpath.internal .XPath.(Unknown Source) at com.sun.org.apache.xpath.internal.XPath.(Unknown Source) at com.sun.org.apache.xpath.internal.jaxp.XPathImpl.eval(Unknown Source) at com .sun.org.apache.xpath.internal.jaxp.XPathImpl.evaluate(Unknown Source) at org.dom.Dom.main(Dom.java:78) ---------------链接到 ------------------ javax.xml.xpath。XPathExpressionException at com.sun.org.apache.xpath.internal.jaxp.XPathImpl.evaluate(Unknown Source) at org.dom.Dom.main(Dom.java:78) 原因:javax.xml.transform.TransformerException:引用错误字面意思...预期的单引号!在 com.sun.org.apache.xpath.internal.compiler.XPathParser.error(Unknown Source) 在 com.sun.org.apache.xpath.internal.compiler.Lexer.tokenize(Unknown Source) 在 com.sun.org .apache.xpath.internal.compiler.Lexer.tokenize(Unknown Source) at com.sun.org.apache.xpath.internal.compiler.XPathParser.initXPath(Unknown Source) at com.sun.org.apache.xpath.internal .XPath.(Unknown Source) at com.sun.org.apache.xpath.internal.XPath.(Unknown Source) at com.sun.org.apache.xpath.internal.jaxp.XPathImpl.eval(Unknown Source) .. . 2 更多 internal.jaxp.XPathImpl.evaluate(Unknown Source) at org.dom.Dom.main(Dom.java:78) 原因:javax.xml.transform.TransformerException:错误引用的文字...预期的单引号!在 com.sun.org.apache.xpath.internal.compiler.XPathParser.error(Unknown Source) 在 com.sun.org.apache.xpath.internal.compiler.Lexer.tokenize(Unknown Source) 在 com.sun.org .apache.xpath.internal.compiler.Lexer.tokenize(Unknown Source) at com.sun.org.apache.xpath.internal.compiler.XPathParser.initXPath(Unknown Source) at com.sun.org.apache.xpath.internal .XPath.(Unknown Source) at com.sun.org.apache.xpath.internal.XPath.(Unknown Source) at com.sun.org.apache.xpath.internal.jaxp.XPathImpl.eval(Unknown Source) .. . 2 更多 internal.jaxp.XPathImpl.evaluate(Unknown Source) at org.dom.Dom.main(Dom.java:78) 原因:javax.xml.transform.TransformerException:错误引用的文字...预期的单引号!在 com.sun.org.apache.xpath.internal.compiler.XPathParser.error(Unknown Source) 在 com.sun.org.apache.xpath.internal.compiler.Lexer.tokenize(Unknown Source) 在 com.sun.org .apache.xpath.internal.compiler.Lexer.tokenize(Unknown Source) at com.sun.org.apache.xpath.internal.compiler.XPathParser.initXPath(Unknown Source) at com.sun.org.apache.xpath.internal .XPath.(Unknown Source) at com.sun.org.apache.xpath.internal.XPath.(Unknown Source) at com.sun.org.apache.xpath.internal.jaxp.XPathImpl.eval(Unknown Source) .. . 2 更多 transform.TransformerException:错误引用的文字...预期的单引号!在 com.sun.org.apache.xpath.internal.compiler.XPathParser.error(Unknown Source) 在 com.sun.org.apache.xpath.internal.compiler.Lexer.tokenize(Unknown Source) 在 com.sun.org .apache.xpath.internal.compiler.Lexer.tokenize(Unknown Source) at com.sun.org.apache.xpath.internal.compiler.XPathParser.initXPath(Unknown Source) at com.sun.org.apache.xpath.internal .XPath.(Unknown Source) at com.sun.org.apache.xpath.internal.XPath.(Unknown Source) at com.sun.org.apache.xpath.internal.jaxp.XPathImpl.eval(Unknown Source) .. . 2 更多 transform.TransformerException:错误引用的文字...预期的单引号!在 com.sun.org.apache.xpath.internal.compiler.XPathParser.error(Unknown Source) 在 com.sun.org.apache.xpath.internal.compiler.Lexer.tokenize(Unknown Source) 在 com.sun.org .apache.xpath.internal.compiler.Lexer.tokenize(Unknown Source) at com.sun.org.apache.xpath.internal.compiler.XPathParser.initXPath(Unknown Source) at com.sun.org.apache.xpath.internal .XPath.(Unknown Source) at com.sun.org.apache.xpath.internal.XPath.(Unknown Source) at com.sun.org.apache.xpath.internal.jaxp.XPathImpl.eval(Unknown Source) .. . 2 更多