 |
 |
 |
Hi Nicholas,
Thanks for responding to my question.Â
I can confirm that the XPath using Saxon parser ("org.ccil.cowan.tagsoup.Parser") is working with default namespace. I made the mistake of assuming that the XML document converted by TagSoup was identical to using light_html2xml in the past.Â
Consequently, what is outstanding still, even though not critical, but nice to have, is ( i ) to exclude DTD from XML file. If this is not possible, ( iv ) to setup local SYSTEM EntityResolver in this JDOM environment.
Below is an example of what I am trying to achieve in ( iv ) in a DOM environment:
Â
import java.io.IOException;
import java.io.StringReader;
Â
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
Â
import org.w3c.dom.Document;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
Â
Â
public class ParseExamples
{
   private final static String COMMON_XML
       = "<music>"
       +    "<artist name=\"Anderson, Laurie\">"
       +        "<album>Big Science</album>"
       +        "<album>Strange Angels</album>"
       +    "</artist>"
       +    "<artist name=\"Fine Young Cannibals\">"
       +        "<album>The Raw & The Cooked</album>"
       +    "</artist>"
       + "</music>";
Â
Â
   private final static String COMMON_DTD
       = "<!ELEMENT music (artist*)>"
       + "<!ELEMENT artist (album+)>"
       + "<!ELEMENT album (#PCDATA)>"
       + "<!ATTLIST artist name CDATA #REQUIRED>";
Â
Â
Â
   public static void main(String[] argv)
   throws Exception
   {
       // this version uses just a SYSTEM identifier - note that it gets turned
       // into a file: URL
       String xml = "<!DOCTYPE music SYSTEM \"bar\">"
                  + COMMON_XML;
Â
       // this version uses both PUBLIC and SYSTEM identifiers; the SYSTEM ID
       // gets munged, the PUBLIC ID doesn't
//Â Â Â Â Â Â Â String xml = "<!DOCTYPE music PUBLIC \"foo\" \"bar\">"
//Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â + COMMON_XML;
Â
       DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
       dbf.setValidating(true);
       DocumentBuilder db = dbf.newDocumentBuilder();
       db.setEntityResolver(new EntityResolver()
       {
           public InputSource resolveEntity(String publicId, String systemId)
               throws SAXException, IOException
           {
               System.out.println("publicId = " + publicId);
               System.out.println("systemId = " + systemId);
               return new InputSource(new StringReader(COMMON_DTD));
           }
       });
Â
       Document dom = db.parse(new InputSource(new StringReader(xml)));
       System.out.println("root element name = " + dom.getDocumentElement().getNodeName());
   }
}
Would anyone be able to give me some idea on how to do this?
Thanks a lot again,
Jack
________________________________
From: Nicholas Ardlie <nicholas.ardlie@p...>
To: netbeansfan@y...; xml-dev@l...
Sent: Thursday, 6 November, 2008 9:40:38 PM
Subject: RE: [xml-dev] How to parse XML document with default namespace with JDOM XPath
Jack,
It seems to be only the Ant build for Tagsoup that requires Saxon 6.5.
You can run the binary distribution (tagsoup-1.2.jar) without Saxon and, as
Michael has pointed out, the code you posted has no dependency on Saxon.
You may have an environment or classpath issue given that the following
class (which is your code with typo 'saxbuilder' changed to 'saxBuilder'),
outputs what you are expecting:
===================
OUTPUT:
===================
hollywood
san jose
san francisco
San diego
===================
JAVA CLASS:
===================
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Iterator;
import java.util.List;
import org.jdom.Content;
import org.jdom.input.SAXBuilder;
import org.jdom.xpath.XPath;
public class Test {
   @SuppressWarnings("unchecked")
   public static void main(String[] args) {
      try {
         FileReader frInHtml = new
FileReader("C:\\Temp\\ABC.html");
         BufferedReader brInHtml = new BufferedReader(frInHtml);
         SAXBuilder saxBuilder = new SAXBuilder(
               "org.ccil.cowan.tagsoup.Parser");
         org.jdom.Document jdomDocument =
saxBuilder.build(brInHtml);
         XPath xpath = XPath
.newInstance("/ns:html/ns:body/ns:div[@id='container']/ns:div[@id='content']
/ns:table[@class='sresults']/ns:tr/ns:td/ns:a");
         xpath.addNamespace("ns", "http://www.w3.org/1999/xhtml");
         List list = (List) (xpath.selectNodes(jdomDocument));
         Iterator iterator = list.iterator();
         while (iterator.hasNext()) {
            Object object = iterator.next();
            if (object instanceof Content)
               System.out.println(((Content)
object).getValue());
         }
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}
===================
INPUT FILE:
===================
<!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" />
</head>
<body>
  <div id="container">
    <div id="content">
      <table class="sresults">
        <tr>
          <td>
            <a href="http://www.abc.com/areas"
title="Hollywood, CA">hollywood</a>
          </td>
          <td>
            <a href="http://www.abc.com/areas" title="San Jose,
CA">san jose</a>
          </td>
          <td>
            <a href="http://www.abc.com/areas" title="San
Francisco, CA">san francisco</a>
          </td>
          <td>
            <a href="http://www.abc.com/areas" title="San
Diego, CA">San diego</a>
          </td>
       </tr>
      </table>
    </div>
  </div>
</body>
</html>
===================
BUILD/RUN PATH:
===================
JDK 1.6.0_06 (i.e. xerces & xalan)
tagsoup-1.2.jar
jdom.jar
jaxen-core.jar
jaxen-jdom.jar
saxpath.jar
Regards,
Nick Ardlie.
Search 1000's of available singles in your area at the new Yahoo!7 Dating. Get Started http://au.dating.yahoo.com/?cid=53151&pid=1011
|
 | 

|  |
These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.
|  |
| |
 |
 |
 |