I'm trying to add an element (javascript) using DOM at the specific location (After the div tag where id="123") of an XHTML file, which I'm doing with getElementById(). So for locating the position of the element I have used two approaches. 1: Without using XPath (Which I have commented in my code) 2: With XPath (After suggestions in the forum) From the first approach I'm able to get the value of the corresponding class, where id="123" but its throwing NullPointer exception also. So I used XPath but while using evaluate(String, Object) method its showing error. Can anyone please tell me that what argument I need to pass here along with 'sxp' --> NodeList nodes = xPath.evaluate(sxp); Thanking you!
My 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 Code:
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
....
.....
}
}
Error which I'm getting-
Exception in thread "main" javax.xml.transform.TransformerException: misquoted literal... expected single quote! at com.sun.org.apache.xpath.internal.compiler.XPathParser.error(Unknown Source) at com.sun.org.apache.xpath.internal.compiler.Lexer.tokenize(Unknown Source) at 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) --------------- linked to ------------------ javax.xml.xpath.XPathExpressionException at com.sun.org.apache.xpath.internal.jaxp.XPathImpl.evaluate(Unknown Source) at org.dom.Dom.main(Dom.java:78) Caused by: javax.xml.transform.TransformerException: misquoted literal... expected single quote! at com.sun.org.apache.xpath.internal.compiler.XPathParser.error(Unknown Source) at com.sun.org.apache.xpath.internal.compiler.Lexer.tokenize(Unknown Source) at 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 more