[XML-DEV Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

RE: How to parse XML document with default namespace with JDOM

  • From: "Michael Kay" <mike@s...>
  • To: "'Jack Bush'" <netbeansfan@y...>,<xml-dev@l...>
  • Date: Tue, 4 Nov 2008 13:39:48 -0000

RE:  How to parse XML document with default namespace with JDOM
I see no Saxon code here. You are using the XPath engine that comes with JDOM. You might be better off asking on the JDOM list. I have to confess I'm surprised to see you declaring namespaces AFTER compiling the XPath expression, but I can't say I'm familiar with this API.
Michael Kay

From: Jack Bush [mailto:netbeansfan@y...]
Sent: 04 November 2008 13:02
To: xml-dev@l...
Subject: How to parse XML document with default namespace with JDOM XPath

Hi All,


I am having difficulty parsing using Saxon and TagSoup parser on a namespace html document. The relevant content of this document are as follows:


<!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">


<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />




    <div id="container">

        <div id="content">

            <table class="sresults">



                        <a href="http://www.abc.com/areas" title="Hollywood, CA">hollywood</a>



                        <a href="http://www.abc.com/areas" title="San Jose, CA">san jose</a>



                        <a href="http://www.abc.com/areas" title="San Francisco, CA">san francisco</a>



                        <a href="http://www.abc.com/areas" title="San Diego, CA">San diego</a>







Below is the relevant code snippets illustrates how I have attempted to retrieve the contents (value of  <a>):


             import java.util.*;

             import org.jdom.*;

             import org.jdom.xpath.*;

             import org.saxpath.*;

             import org.ccil.cowan.tagsoup.Parser;


( 1 )       frInHtml = new FileReader("C:\\Tmp\\ABC.html");

( 2 )       brInHtml = new BufferedReader(frInHtml);

( 3 ) //    SAXBuilder saxBuilder = new SAXBuilder("org.apache.xerces.parsers.SAXParser");

( 4 )       SAXBuilder saxBuilder = new SAXBuilder("org.ccil.cowan.tagsoup.Parser");

( 5 )       org.jdom.Document jdomDocument = saxbuilder.build(brInHtml);

( 6 )       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");

( 7 )       xpath.addNamespace("ns", "http://www.w3.org/1999/xhtml");

( 8 )       java.util.List list = (java.util.List) (xpath.selectNodes(jdomDocument));

( 9 )       Iterator iterator = list.iterator();

( 10 )     while (iterator.hasNext())

( 11 )     {

( 12 )            Object object = iterator.next();

( 13 ) //         if (object instanceof Element)

( 14 ) //               System.out.println(((Element)object).getTextNormalize());

( 15 )             if (object instanceof Content)

( 16 )                   System.out.println(((Content)object).getValue());




This program would work on the same document without the default namespace, hence, it would not be necessary to include “ns” prefix along in the XPath statements (line 6-7) either. Moreover, I was using “org..apache.xerces.parsers.SAXParser” to have successfully retrieve content of <a> from the same document without default namespace in the past.


I would like to achieve the following objectives if possible:


( i ) Exclude DTD and namespace in order to simplifying the parsing process. How this could be done?

( ii ) If this is not possible, how to include it in XPath statements (line 6-7) so that the value of <a> is picked up correctly?

( iii ) Would changing from “org.apache.xerces.parsers.SAXParser” to “org.ccil.cowan.tagsoup.Parser” make any difference as far as using XPath is concerned?

( iv ) Failing to exlude DTD, how to change the lookup of a PUBLIC DTD to a local SYSTEM one and include a local DTD for reference?


I am running JDK 1.6.0_06, Netbeans 6.1, JDOM 1.1, Saxon6-5-5, Tagsoup 1.2 on Windows XP platform.


Any assistance would be appreciated.


Thanks in advance,



Search 1000's of available singles in your area at the new Yahoo!7 Dating. http://au.rd.yahoo.com/dating/mail/tagline1/*http://au.dating.yahoo.com/?cid=53151&pid=1011.

[Date Prev] | [Thread Prev] | [Thread Next] | [Date Next] -- [Date Index] | [Thread Index]


Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
First Name
Last Name
Subscribe in XML format
RSS 2.0
Atom 0.3

Stylus Studio has published XML-DEV in RSS and ATOM formats, enabling users to easily subcribe to the list from their preferred news reader application.

Stylus Studio Sponsored Links are added links designed to provide related and additional information to the visitors of this website. they were not included by the author in the initial post. To view the content without the Sponsor Links please click here.

Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.