read an xml file and write it back in an eclipse plugin application

  • Thread starter summer_good_feeling
  • Start date
S

summer_good_feeling

Hi everyone,
I'm writing a eclipse plugin. I need to read an xml file, do some
modification and write it back. But I have problem to locate the file
when I write back.

My code is following:

/* read and parser part (work fine) */
DOMParser parser = new DOMParser();

parser.setFeature("http://xml.org/sax/features/validation", true);
System.out.println("filename: " + fileName);

String fileName =
this.getClass().getResource("registry.xml").toString();
parser.parse(fileName);

doc = parser.getDocument();

/* write back */
StreamResult result = new StreamResult(new FileWriter(fileName));

TransformerFactory transFactory = TransformerFactory.newInstance();
Transformer transformer;
transformer = transFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
transformer.transform(source, result);

then it throws error:
java.io.FileNotFoundException:
bundleresource:\423\dir\model\registry.xml (The filename, directory
name, or volume label syntax is incorrect)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(Unknown Source)
at java.io.FileOutputStream.<init>(Unknown Source)
at java.io.FileWriter.<init>(Unknown Source)
at dir.RegistryDetail.update(RegistryDetail.java:221)
at dir.SettingPanel$4.widgetSelected(SettingPanel.java:201)
at
org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListener.java:90)

at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:66)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:843)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:3080)

at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:2713)
at
edu.uga.cs.lsdis.uddi.editor.SettingPanel.createPanel(SettingPanel.java:91)

at edu.uga.cs.lsdis.uddi.editor.UDDIEditor$3.run(UDDIEditor.java:157)
at org.eclipse.jface.action.Action.runWithEvent(Action.java:996)
at
org.eclipse.jface.action.ActionContributionItem.handleWidgetSelection(ActionContributionItem.java:538)

at
org.eclipse.jface.action.ActionContributionItem.access$2(ActionContributionItem.java:488)

at
org.eclipse.jface.action.ActionContributionItem$5.handleEvent(ActionContributionItem.java:400)

at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:66)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:843)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:3080)

at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:2713)
at org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.java:1699)
at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:1663)
at
org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:367)

at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:143)

at
org.eclipse.ui.internal.ide.IDEApplication.run(IDEApplication.java:103)

at
org.eclipse.core.internal.runtime.PlatformActivator$1.run(PlatformActivator.java:226)

at
org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:376)

at
org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:163)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.core.launcher.Main.invokeFramework(Main.java:334)
at org.eclipse.core.launcher.Main.basicRun(Main.java:278)
at org.eclipse.core.launcher.Main.run(Main.java:973)
at org.eclipse.core.launcher.Main.main(Main.java:948)

Can anyone help me out? Thanks in advance!!!
 
K

kookey

Hello! In fact, I cann't answer your question though I took 1 hour to
find an answer. Because nowadays I am studing DOM for a test blog. Both
are the same.
I think: your class DOMParser implements interface XMLReader, method
setFeature(String str, boolean value) sets the value, method
java.lang.Class.getResource(String str) returns a URL object, and its
toString() method returns fileName.
 
S

summer_good_feeling

yes. I feel so confused because using the fileName, parser can parse
it, but fileWriter fails. I'm playing with URL, URI and File for a
whole day and still can't figure it out. Someone told me it's maybe a
bug of JDK. Anyway, Thanks for reply.
 
T

Thomas Fritsch

String fileName =
this.getClass().getResource("registry.xml").toString();
In order to get the URL you can split up the line above to:
URL url = this.getClass().getResource("registry.xml");
System.out.println("url = " + url);
String fileName = url.toString();
As seen from the exception stack trace below this will probably print
"url = bundleresource:/423/dir/model/registry.xml".
Anyway: only if you get something like "url = file:/..." then you know that
the URL actually is a file URL and the part after "file:" is the file name.
Then there are chances that you could open it with
FileInputStream/FileOutputStream, of course after stripping off the leading
"file:". Otherwise this won't work, simply because it isn't a file name.

Your String-variable fileName is misleading, because it is not a file name.
It is an URL-string. It worked in your case, only because the parse-method
expects an URL-String, not a file-name.
parser.parse(fileName);

doc = parser.getDocument();

/* write back */
StreamResult result = new StreamResult(new FileWriter(fileName));
[...]

then it throws error:
java.io.FileNotFoundException:
bundleresource:\423\dir\model\registry.xml (The filename, directory
name, or volume label syntax is incorrect)
at java.io.FileOutputStream.open(Native Method) [...]

Can anyone help me out? Thanks in advance!!!
You will have to study the javadoc of class java.net.URL (and also of class
java.net.URLConnection) to find the generic way how to open an OutputStream
to an arbitrary URL. If I recall it correctly, it is roughly like follows:
URLConnection urlConnection = url.openConnection();
urlConnection.setDoOutput(true);
OutputStream outputStream = urlConnection.getOutputStream();
....
But be warned: Your special URL-protocol-handler (the one that processes all
URLs beginning with "bundleresource:") might well support input only, but
not output. You would get a self-explaining exception then. If that is true,
then you have lost.
 
S

summer_good_feeling

I tried URLConnection. It doesn't work. The exception is:
java.net.UnknownServiceException: protocol doesn't support output
at java.net.URLConnection.getOutputStream(URLConnection.java:785)

I think that's because URLConnection is an abstract class extended by
HttpURLConnection and JarURLConnection. It can't support for other
format files.
 
T

Thomas Fritsch

I tried URLConnection. It doesn't work. The exception is:
java.net.UnknownServiceException: protocol doesn't support output
at java.net.URLConnection.getOutputStream(URLConnection.java:785)

I think that's because URLConnection is an abstract class extended by
HttpURLConnection and JarURLConnection. It can't support for other
format files.
No, that is not the reason! When calling
URLConnection urlConnection = url.openConnection();
you do not get an *abstract* URLConnection object as such.
Instead you get an object of some *concrete* subclass extending from
URLConnection (that is how polymorphy works in any OO language). Might
be you get a com.ibm.IDontKnowWhatURLConnection.
Anyway, the developers of this URLConnection subclass decided not to
support output, and so they didn't implement a getOutputStream() method
of their own. And therefore the dumb getOutputStream() method of the
URLConnection base class remains in effect.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top