Solved

html to xhtml

Posted on 2009-07-10
4
249 Views
Last Modified: 2012-05-07
How would I convert the html string to xhtml before parsing it.  The below works fine, unless I take out the quotes around 'white', making it non-xhtml compliant.  Would Tidy do the job?  If so, how would I code such?
<%@ 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");

}

%>

Open in new window

0
Comment
Question by:arichexe
  • 3
4 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 24829670
0
 

Author Comment

by:arichexe
ID: 24830927
How would I modify my code to utilize Tidy?
<%@ 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");

}

%>

Open in new window

0
 
LVL 86

Accepted Solution

by:
CEHJ earned 200 total points
ID: 24831126
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24902022
:-)
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now