Hello all,
I can't seem to get XPATH to work properly. The only thing I've been
able to select is the root node.
Any help is much apreciated.
Thanks,
--Evan
Here's my HTML, code, and input/output. I think I must be doing
something fundamentally wrong, but can't seem to spot it.
test.html:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Title</title>
<style>
body{
background: blue;
}
</style>
</head>
<body>
<div id="main">
HI!
</div>
</body>
</html>
CODE:(note: args[0] is the file, args[1] is the XPath expression)
Document doc = DOM.getCachedDocument(args[0]);
Element root = doc.getRootElement();
List l = root.selectNodes(args[1]);
if(l.size() > 0){
Element elem = (Element)l.get(0);
Element p = elem.addElement("p");
p.setText("hi");
}else{
System.out.println(" NO MATCHES! ");
}
DOM.print( doc, new PrintWriter(System.out, true) );
TRIES:
./run.sh test.html /html/body
NO MATCHES!
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Title</title>
<style xml:space="preserve">
body{
background: blue;
}
</style>
</head>
<body>
<div id="main">
HI!
</div>
</body>
</html>
./run.sh test.html *
NO MATCHES!
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Title</title>
<style xml:space="preserve">
body{
background: blue;
}
</style>
</head>
<body>
<div id="main">
HI!
</div>
</body>
</html>
This one works:
./run.sh test.html .
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Title</title>
<style xml:space="preserve">
body{
background: blue;
}
</style>
</head>
<body>
<div id="main">
HI!
</div>
</body>
<p>hi</p>
</html>
I don't really understand this either. If the context node doesn't
throw this, why would the root node?
./run.sh test.html /
java.lang.ClassCastException
at edu.uark.util.dom.DOMTest.main(DOMTest.java:17)
-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems? Stop! Download the new AJAX search engine that makes
searching your log files as easy as surfing the web. DOWNLOAD SPLUNK!
http://ads.osdn.com/?ad_id=7637&alloc_id=16865&op=click
_______________________________________________
dom4j-user mailing list
dom4j-user@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/dom4j-user