473,666 Members | 2,449 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.sourc eforge site. So far so good.

However, if I unplug the network, I get (after a *long* timeout) a
java.net.Socket Exception. 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(S tring 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.ne wInstance().new XPath();
final String expression = "//property";
final String completePath = ... //file path
InputSource inputSource = new InputSource(com pletePath);
DTMNodeList nodes = (DTMNodeList) xpath.evaluate( expression,
inputSource, XPathConstants. NODESET);

Any ideas?
TIA
andy

Sep 15 '06 #1
3 1874


ab*****@yahoo.c o.uk wrote:

However, if I unplug the network, I get (after a *long* timeout) a
java.net.Socket Exception. 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
setEntityResolv er. 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 =
DocumentBuilder Factory.newInst ance().newDocum entBuilder();
db.setEntityRes olver(new UriTransform()) ;
final String completePath = //file path
Document doc = db.parse(new File(completePa th));
XPath xpath = XPathFactory.ne wInstance().new XPath();
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
1951
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 printing a document. I can see how you can do this in JDK1.4 but not in JDK1.3. Is it possible? Are there any other libraries that I could use instead that do support this functionality?
0
1853
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, but was able to fix it (or so I thought) by using wget instead of Firefox. After installing the JDK (/opt/jdk1.5.0) I pointed JBuilder to it. When I try to compile any code, I get the following error:
0
1803
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 on Java 2 (JDK1.2 or later) But I am using jdk1.4 Though I have changed the environmental variable from system:
4
21202
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 native English speaker, it's quite hard to make the problem clear. Please see the following example.
1
9485
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
4574
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 but it was not considering the Gaps.., Example: String Str="1,XYZ,Deltastreet,,US"; when i use in split method by giving split(",");working fine and storing a array.
1
1845
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 making changes in font.properties file.I have tried it myself and works too. But I am not aware the procedures to be followed in jdk1.6
2
4981
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 command C:\>cd jdk1.6.02_02 it says invalid directory.what is the problem??? pls help me. Thanx in advance.
1
3699
by: jitenid | last post by:
What is the difference between jdk1.5 & jdk1.6 in relate to Applet
0
8440
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
8549
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8636
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7378
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5661
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4358
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2765
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2005
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1763
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.