getElementById() を使用して、XHTML ファイルの特定の場所 (id="123" の div タグの後) に DOM を使用して要素 (javascript) を追加しようとしています。そのため、要素の位置を特定するために、2 つのアプローチを使用しました。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 (不明なソース) com.sun.org.apache.xpath.internal.compiler.Lexer.tokenize (不明なソース) com.sun.org .apache.xpath.internal.compiler.Lexer.tokenize (不明なソース) com.sun.org.apache.xpath.internal.compiler.XPathParser.initXPath (不明なソース) com.sun.org.apache.xpath.internal .XPath.(未知のソース) com.sun.org.apache.xpath.internal.XPath.(未知のソース) com.sun.org.apache.xpath.internal.jaxp.XPathImpl.eval (未知のソース) com .sun.org.apache.xpath.internal.jaxp.XPathImpl.evaluate(不明なソース) 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: misquotedリテラル...一重引用符が必要です!com.sun.org.apache.xpath.internal.compiler.XPathParser.error (不明なソース) com.sun.org.apache.xpath.internal.compiler.Lexer.tokenize (不明なソース) com.sun.org .apache.xpath.internal.compiler.Lexer.tokenize (不明なソース) com.sun.org.apache.xpath.internal.compiler.XPathParser.initXPath (不明なソース) com.sun.org.apache.xpath.internal .XPath.(不明なソース) com.sun.org.apache.xpath.internal.XPath.(不明なソース) com.sun.org.apache.xpath.internal.jaxp.XPathImpl.eval(不明なソース) .. 。 2以上 org.dom.Dom.main(Dom.java:78) の internal.jaxp.XPathImpl.evaluate(不明なソース) 原因: javax.xml.transform.TransformerException: 誤った引用符のリテラル...予期された単一引用符! com.sun.org.apache.xpath.internal.compiler.XPathParser.error (不明なソース) com.sun.org.apache.xpath.internal.compiler.Lexer.tokenize (不明なソース) com.sun.org .apache.xpath.internal.compiler.Lexer.tokenize (不明なソース) com.sun.org.apache.xpath.internal.compiler.XPathParser.initXPath (不明なソース) com.sun.org.apache.xpath.internal .XPath.(不明なソース) com.sun.org.apache.xpath.internal.XPath.(不明なソース) com.sun.org.apache.xpath.internal.jaxp.XPathImpl.eval(不明なソース) .. 。 2以上 org.dom.Dom.main(Dom.java:78) の internal.jaxp.XPathImpl.evaluate(不明なソース) 原因: javax.xml.transform.TransformerException: 誤った引用符のリテラル...予期された単一引用符! com.sun.org.apache.xpath.internal.compiler.XPathParser.error (不明なソース) com.sun.org.apache.xpath.internal.compiler.Lexer.tokenize (不明なソース) com.sun.org .apache.xpath.internal.compiler.Lexer.tokenize (不明なソース) com.sun.org.apache.xpath.internal.compiler.XPathParser.initXPath (不明なソース) com.sun.org.apache.xpath.internal .XPath.(不明なソース) com.sun.org.apache.xpath.internal.XPath.(不明なソース) com.sun.org.apache.xpath.internal.jaxp.XPathImpl.eval(不明なソース) .. 。 2以上 transform.TransformerException: 誤って引用されたリテラル...一重引用符が必要です! com.sun.org.apache.xpath.internal.compiler.XPathParser.error (不明なソース) com.sun.org.apache.xpath.internal.compiler.Lexer.tokenize (不明なソース) com.sun.org .apache.xpath.internal.compiler.Lexer.tokenize (不明なソース) com.sun.org.apache.xpath.internal.compiler.XPathParser.initXPath (不明なソース) com.sun.org.apache.xpath.internal .XPath.(不明なソース) com.sun.org.apache.xpath.internal.XPath.(不明なソース) com.sun.org.apache.xpath.internal.jaxp.XPathImpl.eval(不明なソース) .. 。 2以上 transform.TransformerException: 誤って引用されたリテラル...一重引用符が必要です! com.sun.org.apache.xpath.internal.compiler.XPathParser.error (不明なソース) com.sun.org.apache.xpath.internal.compiler.Lexer.tokenize (不明なソース) com.sun.org .apache.xpath.internal.compiler.Lexer.tokenize (不明なソース) com.sun.org.apache.xpath.internal.compiler.XPathParser.initXPath (不明なソース) com.sun.org.apache.xpath.internal .XPath.(不明なソース) com.sun.org.apache.xpath.internal.XPath.(不明なソース) com.sun.org.apache.xpath.internal.jaxp.XPathImpl.eval(不明なソース) .. 。 2以上