Be seen. Boost your question’s priority for more expert views and faster solutions
<%@ page import="java.io.*,java.net.*,java.text.*,java.util.*,javax.xml.parsers.*,javax.xml.xpath.*,org.w3c.dom.*,org.w3c.dom.*,org.xml.sax.*" %>
<%
String htm;
htm = "<html>" +
"<body bgcolor='white'>" +
"<head>" +
"<title>Hello World</title>" +
"</head>" +
"</body>" +
"</html>";
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(false);
factory.setIgnoringElementContentWhitespace(true);
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(new InputSource(new StringReader(htm)));
document.getDocumentElement().normalize();
XPath xpath = XPathFactory.newInstance().newXPath();
NodeList nodeList = (NodeList) xpath.evaluate("//title/text()",document,XPathConstants.NODESET);
if (nodeList.getLength() > 0) {
for (int i = 0; i < nodeList.getLength(); i++) {
out.print(nodeList.item(i).toString());
}
}else{
out.print("not found");
}
%>
<%@ page import="java.io.*,java.net.*,java.text.*,java.util.*,javax.xml.parsers.*,javax.xml.xpath.*,org.w3c.dom.*,org.w3c.dom.*,org.xml.sax.*" %>
<%
String htm;
htm = "<html>" +
"<body bgcolor='white'>" +
"<head>" +
"<title>Hello World</title>" +
"</head>" +
"</body>" +
"</html>";
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(false);
factory.setIgnoringElementContentWhitespace(true);
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(new InputSource(new StringReader(htm)));
document.getDocumentElement().normalize();
XPath xpath = XPathFactory.newInstance().newXPath();
NodeList nodeList = (NodeList) xpath.evaluate("//title/text()",document,XPathConstants.NODESET);
if (nodeList.getLength() > 0) {
for (int i = 0; i < nodeList.getLength(); i++) {
out.print(nodeList.item(i).toString());
}
}else{
out.print("not found");
}
%>
Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.
Have a better answer? Share it in a comment.
Join the community of 500,000 technology professionals and ask your questions.