473,383 Members | 1,829 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

JDK1.5 Xpath problem

Hi there,

I'm using, for the first time, the JDK1.5 Xpath API. I need to find
elements in a Hibernate-generated .hbm.xml file. These files come with
a <!DOCTYPE header mentioning a remote URL. The Xpath parser fetches
the URL from the hibernate.sourceforge site. So far so good.

However, if I unplug the network, I get (after a *long* timeout) a
java.net.SocketException. I looked around and I found out that I have
to define a class implementing EntityResolver to change the default
behaviour (i.e., fetch the DTD over the net) and obtain an InputSource
from it. Something along the lines of:

public class UriTransform implements EntityResolver {
public InputSource resolveEntity(String publicId, String systemId) {
return new InputSource(new StringReader(""));
}
}

Now my problem is: How do I do it? Neither in javax.xml.xpath.XPath nor
in javax.xml.xpath.XPathFactory did I find an appropriate place nor did
I find a method to gain access to the underlying SAX parser. Here's my
code:

XPath xpath = XPathFactory.newInstance().newXPath();
final String expression = "//property";
final String completePath = ... //file path
InputSource inputSource = new InputSource(completePath);
DTMNodeList nodes = (DTMNodeList) xpath.evaluate(expression,
inputSource, XPathConstants.NODESET);

Any ideas?
TIA
andy

Sep 15 '06 #1
3 1860


ab*****@yahoo.co.uk wrote:

However, if I unplug the network, I get (after a *long* timeout) a
java.net.SocketException. I looked around and I found out that I have
to define a class implementing EntityResolver to change the default
behaviour (i.e., fetch the DTD over the net) and obtain an InputSource
from it.
Now my problem is: How do I do it? Neither in javax.xml.xpath.XPath nor
in javax.xml.xpath.XPathFactory did I find an appropriate place nor did
I find a method to gain access to the underlying SAX parser.
I don't know how to do it if you pass an InputSource to the evaluate
method. You can however also pass in an object where the object is a W3C
DOM Node. And if you look at DocumentBuilder it has a method
setEntityResolver. That should allow you to create a DOM Document
without fetching the DTD from the remote host, and you can then pass in
the Document to the evaluate method. I am not sure a DOM Node/Document
is the most efficient data structure to do XPath on but at least that
approach might work for your problem.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Sep 15 '06 #2
One way around this is to instantiate the parser yourself, configure it
appropriately, then pass it to the transformer wrapped in a SAXSource.

(There ought to be a way to pass an entity resolver/URI resolver through
the XPath APIs, but I'm having trouble finding it.)
Sep 15 '06 #3
Martin Honnen wrote:
You can however also pass in an object where the object is a W3C
DOM Node.
Thank you so much Martin, that actually fixed my problem! The following
code now works with and without a network connection:
---------------------------------
DocumentBuilder db =
DocumentBuilderFactory.newInstance().newDocumentBu ilder();
db.setEntityResolver(new UriTransform());
final String completePath = //file path
Document doc = db.parse(new File(completePath));
XPath xpath = XPathFactory.newInstance().newXPath();
final String expression = "//property";
DTMNodeList nodes = (DTMNodeList) xpath.evaluate(expression, doc,
XPathConstants.NODESET);
---------------------------------

Sep 16 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Gary Johnson | last post by:
Hi All Having looked at the Java Docs (JDK 1.3) in java.awt.print.*, I can't see if it is possible to (a) get a list of available Windows printer queues and (b) select the printer queue when...
0
by: Nicholas Pappas | last post by:
Hello all. I am having some trouble getting JDK1.5 working under Gentoo Linux, and am hoping someone might know a few tricks. I previously had some problems getting the .BIN to work at all,...
0
by: s | last post by:
Hi, I am a new user to j2sdkee1.2.1 After installing this I couldn't run this command "j2ee -verbose" it shows following error: C:\>j2ee -verbose Fatal Error: This J2EE SDK release runs only...
4
by: Son KwonNam | last post by:
In XSLT, is this possible to get value from xml using XPath which is in XSLT variable? I mean XPath strings can be dynamic while XSL Transforming. If possible, How?? Because I'm not a...
1
by: rameshkumarc | last post by:
can anyone plz tell abt the difference between jdk1.5 and jdk1.6..what are all improvement made in jdk1.6
5
by: chindanoor | last post by:
As i used split method in my local Machine(jdk1.4) it was working fine.., When i try to use in the server(which is jdk1.3 version) unable to support it.., As i m able to do it with StringTokenizer...
1
by: pushpa13 | last post by:
Internationalization in java can enable a user to create application which can have the interfaces available in all the languagues. In earlier version of jdk1.4.1 and jdk1.4.2 that was done by...
2
by: blue2007i | last post by:
hi, every 1 i am a student.pls help me.i have jdk1.6.0_02 and also Bluej 2.2.0 i am able to wtite program in bluej and also compile and get desired output but i cant write program in DOS. when i give...
1
by: jitenid | last post by:
What is the difference between jdk1.5 & jdk1.6 in relate to Applet
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.