开发者

XML or text declaration not at start of entity - java

I use Java and call a webservice. When I display the response on my web page with :

String str=answer.toString();
label.setText(str);

It's okay. But when I want to call a开发者_JAVA技巧 JavaScript function, I fail. I used a code which worked ( here ) and changed it to do what I want. So in the beginning of my code I have

import netscape.javascript.JSException;
import netscape.javascript.JSObject;

then later

        try {
        jso = JSObject.getWindow(this);  // line 38
    } catch (JSException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

then later

jso.call("affiche1",new String[] {String.valueOf(str.trim()).trim()}); // line 110

"affiche" is my js function which should display the answer on my web page.

When I run it with Eclipse I get the errors

netscape.javascript.JSException at netscape.javascript.JSObject.getWindow(Unknown Source) at Test2.init(Test2.java:38) at sun.applet.AppletPanel.run(Unknown Source) at java.lang.Thread.run(Unknown Source)

java.lang.NullPointerException at Test2$1.actionPerformed(Test2.java:110) at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.setPressed(Unknown Source) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source) at java.awt.Component.processMouseEvent(Unknown Source) at javax.swing.JComponent.processMouseEvent(Unknown Source) at java.awt.Component.processEvent(Unknown Source) at java.awt.Container.processEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source)

The lines 38 and 110 are precised above. When I run it in Mozilla, I get the errors

XML or text declaration not at start of entity Line 1

  "<html xmlns="http://www.w3.org/1999/xh...teReply></soap:Body></soap:Envelope></"

and

An invalid or illegal string was specified" code: "12 file:///L:/pwe/onload_refresh/ngonl/pub/htmlkit/effects.js Line 22

I have not found something similar on the web. I just found advices : to drop the whitespace in my xml file, but I think it is not that because there is no problem when I display it inside the applet (and I don't know exactly how doing that, I am a very beginner in java).

I really don't understand because I have the line

jso.call("affiche1",new String[] {String.valueOf(str.trim()).trim()});

I drop white spaces before and after with trim() - two are not necessary, but I wanted to be sure to drop it ! I think the errors in Eclipse are not important because I got the same with something which works (she may be here because it s calling javascript, and javasript is not in my eclipse project ?).

I find it very strange because I have a string, and not a XML, right ? So I really dont understand why this error is raised...

Thank you very much for any help and advice.


First, the NullPointerException in line 110 - which you highlighted in your sample - can only mean that the jso variable is null, because the rest of that line would not throw a NullPointerException.

Without knowing too much about your application, you should try and see why this is the case. Is it set to a non-null value in all circumstances, before it reaches this line (and not set back to null either)?

In your specific case, just printing out the Javascript Exception with .printStacktrace() will let your program go on, with the jso variable being null. So you must fix the root cause of the Javascript exception - that will make the NullPointerException go away, too. In general, it is better to throw exceptions, instead of just printing them and going on.

Moreover, you should adhere to the advice to remove superfluous whitespace. Especially at the beginning of an XML document you must not have whitespace, otherwise parsers are likely to reject it. An XML file must start with an opening bracket; sometimes even the UTF-8/16/32 byte order marks at the beginning of an XML stream cause problems. They are often introduced by editors during saving, even though you can usually not see them, unless you look at the file in hex view.

Displaying the XML in your Applet is not an indicator of it being correct - you just set the text of a label, which does not care whether it is XML at all, much less if it is valid.


Problem solved :

it was the innerHTML, because there were non-HTML tags inside. So, instead of doing directly a innerHTML, I call this function in javascript :

function test (str) {
    function parseXML( xml ) {
        if( window.ActiveXObject && window.GetObject ) {
            var dom = new ActiveXObject( 'Microsoft.XMLDOM' );
            dom.loadXML( xml );
            return dom;
        }
        if( window.DOMParser )
            return new DOMParser().parseFromString( xml, 'text/xml' );
        throw new Error( 'No XML parser available' );
    }

    str = parseXML(str);
    var supertest = str.getElementsByTagName("tag1")[0].childNodes[0].nodeValue;
    document.getElementById('ici').innerHTML ="value of tag1  :  " + supertest;
}

I parse it (string to dom), then I get the value I want (into tag1 tag), then I edit the html. In fact, that is what I wanted. Testing to display the entire string was not a good idea, and couldnt be done because of the < tag > inside.

Hope it will help someone.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜