Solved

html to xhtml

Posted on 2009-07-10
4
250 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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 scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

910 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

21 Experts available now in Live!

Get 1:1 Help Now