Pagina 1 di 2 1 2 ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 16
  1. #1

    Parsing stringa json all'interno di xml

    Salve, sto cercando di effettuare il parsing di un messaggio xml ricevuto da un webservice che contiene una stringa json. Quando mi arriva il messaggio viene chiamata la funzione error che mi visualizza: "Error: Type: parseerror". E' necessaria qualche funzione particolare per estrarre una stringa json da xml?
    Grazie.

    codice:
    $.ajax({
    	url: productServiceUrl,
    	type: "POST",
    	timeout: 120000,
    	dataType: "xml",
    	data: soapMessage,
    	complete: endSaveProduct,
    	error: function( objAJAXRequest, strError ){
    		$( "h1" ).text(
    		"Error! Type: " +
    		strError
    		);
    		},
    	contentType: "text/xml; charset=\"utf-8\""
    	});
    
    
    	function endSaveProduct(xmlHttpRequest, status)
    	{
     	$(xmlHttpRequest.responseXML)
        .find('ns:matchResponse')
        .each(function()
     	{
       		var name = $(this).find('ns:return').text();
    		$( "h1" ).text(name);
     	})
    	}
    
    	});

  2. #2
    no, 'spetta, com'è lo schema di XML?
    I DON'T Double Click!

  3. #3
    La struttura dell'xml è la seguente:

    codice:
    <?xml version='1.0' encoding='utf-8'?> 
    <soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"> <soapenv:Body> 
    <ns:matchResponse xmlns:ns=\"…\"> 
    <ns:return> 
    [{\"21\":{\"rank\":0.36787944117144233,\"tipo\":\"Maurolico\",\"H\":\"Alice\",\"K\":\"Alice\"}},{\"22\":{\"rank\":0.36787944117144233,\"tipo\":\"Papalina\",\"H\":\"Alice\",\"K\":\"Alice\"}},{\"17\":{\"rank\":0.36787944117144233,\"tipo\":\"Lacerto\",\"H\":\"Alice\",\"K\":\"Alice\"}},{\"18\":{\"rank\":0.36787944117144233,\"tipo\":\"Anguilla\",\"H\":\"Alice\",\"K\":\"Alice\"}},{\"15\":{\"rank\":0.36787944117144233,\"tipo\":\"Capone_gallinella\",\"H\":\"Alice\",\"K\":\"Alice\"}},{\"16\":{\"rank\":0.36787944117144233,\"tipo\":\"Boccanera\",\"H\":\"Alice\",\"K\":\"Alice\"}},{\"13\":{\"rank\":0.36787944117144233,\"tipo\":\"Boccanera\",\"H\":\"Alice\",\"K\":\"Alice\"}},{\"14\":{\"rank\":0.36787944117144233,\"tipo\":\"Lacerto\",\"H\":\"Alice\",\"K\":\"Alice\"}}] 
    </ns:return> 
    </ns:matchResponse> 
    </soapenv:Body> 
    </soapenv:Envelope>

  4. #4
    se puoi modificare la struttura, mettici:

    codice:
    <?xml version='1.0' encoding='utf-8'?> 
    <soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"> <soapenv:Body> 
    <ns:matchResponse xmlns:ns=\"…\"> 
    <ns:return> 
    <![CDATA[ 
    [{\"21\":{\"rank\":0.36787944117144233,\"tipo\":\"Maurolico\",\"H\":\"Alice\",\"K\":\"Alice\"}},{\"22\":{\"rank\":0.36787944117144233,\"tipo\":\"Papalina\",\"H\":\"Alice\",\"K\":\"Alice\"}},{\"17\":{\"rank\":0.36787944117144233,\"tipo\":\"Lacerto\",\"H\":\"Alice\",\"K\":\"Alice\"}},{\"18\":{\"rank\":0.36787944117144233,\"tipo\":\"Anguilla\",\"H\":\"Alice\",\"K\":\"Alice\"}},{\"15\":{\"rank\":0.36787944117144233,\"tipo\":\"Capone_gallinella\",\"H\":\"Alice\",\"K\":\"Alice\"}},{\"16\":{\"rank\":0.36787944117144233,\"tipo\":\"Boccanera\",\"H\":\"Alice\",\"K\":\"Alice\"}},{\"13\":{\"rank\":0.36787944117144233,\"tipo\":\"Boccanera\",\"H\":\"Alice\",\"K\":\"Alice\"}},{\"14\":{\"rank\":0.36787944117144233,\"tipo\":\"Lacerto\",\"H\":\"Alice\",\"K\":\"Alice\"}}] 
     ]]>
    </ns:return> 
    </ns:matchResponse> 
    </soapenv:Body> 
    </soapenv:Envelope>
    In ogni caso, secondo me fai meglio ad usare
    codice:
    var matches = xmlHttpRequest.responseText.match(/<ns:return>(.*)<\/ns:return>/m)
    //matches = ["<ns:return> [{\"21\":{\"rank\":0.36787944117144233, ... </ns:return>",  "[{\"21\":{\"rank  .... \"K\":\"Alice\"}}] "]
    var json = eval(matches[1]);
    N.B: ovviamente se inserisci il <![CDATA[ cambierai anche la RegExp sulla quale fai il match della stringa:

    codice:
    var matches = xmlHttpRequest.responseText.match(/<\!\[CDATA\[(.*)\]\]>/m)
    I DON'T Double Click!

  5. #5
    Purtroppo non posso modificare la struttura. Usando il secondo metodo continua a darmi parsererror.

  6. #6
    scrivimi esattamente il codice che hai usato
    I DON'T Double Click!

  7. #7
    codice:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
            <title>New Web Project</title>
    		<script src="jquery-1.3.2.js" type="text/javascript"></script>
    		<script language="JavaScript" type="text/javascript">
    
    		$(document).ready(function() {
    
    
    	var productServiceUrl = 'http://localhost:8080/Pesce2/proxy2.jsp'; // Preferably write this out from server side
    
    	var soapMessage = ... //messaggio di richiesta al web service
    
    	$.ajax({
    	url: productServiceUrl,
    	type: "POST",
    	timeout: 120000,
    	dataType: "xml",
    	data: soapMessage,
    	complete: endSaveProduct,
    	error: function( objAJAXRequest, strError ){
    		$( "h1" ).text(
    		"Error! Type: " +
    		strError
    		);
    		},
    	contentType: "text/xml; charset=\"utf-8\""
    	});
    
    	function endSaveProduct(xmlHttpRequest, status)
    	{
    		
    		var matches = xmlHttpRequest.responseText.match(/<ns:return>(.*)<\/ns:return>/m);
    		var json = eval(matches[1]);
    		$( "h1" ).text(json);
    
    	}
    
    	});
    
    </script>
    
    		
        </head>
        <body>
            <h1>New Web Project Page</h1>
        </body>
    </html>

  8. #8
    sei sicuro che la risposta di esempio che hai postato sia corretta? Se si, allora usa Firebug con Firefox per vedere la riga dove l'errore è causato.
    I DON'T Double Click!

  9. #9
    Firebug mi dice :

    Errore: la dichiarazione XML o testuale non è all'inizio di un'entità
    File sorgente: http://localhost:8080/Pesce2/proxy2.jsp
    Riga: 3, Colonna: 1
    Codice sorgente:
    <?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><ns:matchResponse xmlns:ns="..."><ns:return>[{"21":{"rank":0.36787944117144233,"tipo":"Maurolic o","H":"Alice","K":"Alice"}},{"22":{"rank":0.36787 944117144233,"tipo":"Papalina","H":"Alice","K":"Al ice"}},{"17":{"rank":0.36787944117144233,"tipo":"L acerto","H":"Alice","K":"Alice"}},{"18":{"rank":0. 36787944117144233,"tipo":"Anguilla","H":"Alice","K ":"Alice"}},{"15":{"rank":0.36787944117144233,"tip o":"Capone_gallinella","H":"Alice","K":"Alice"}},{ "16":{"rank":0.36787944117144233,"tipo":"Boccanera ","H":"Alice","K":"Alice"}},{"13":{"rank":0.367879 44117144233,"tipo":"Boccanera","H":"Alice","K":"Al ice"}},{"14":{"rank":0.36787944117144233,"tipo":"L acerto","H":"Alice","K":"Alice"}}]</ns:return></ns:matchResponse></soapenv:Body></soapenv:Envelope>


    il codice di proxy2.jsp è:

    codice:
    <%@page session="false"%> 
    <%@page import="java.net.*,java.io.*" %> 
    <% try { //String reqUrl = request.getQueryString(); 
    String reqUrl="..."; 
    URL url = new URL(reqUrl); HttpURLConnection con = (HttpURLConnection)url.openConnection(); con.setDoOutput(true); con.setRequestMethod(request.getMethod()); 
    con.setRequestProperty("content-type", "text/xml; charset=utf-8"); 
    int clength = request.getContentLength(); 
    if(clength > 0) { con.setDoInput(true); 
    byte[] idata = new byte[clength]; 
    request.getInputStream().read(idata, 0, clength); 
    con.getOutputStream().write(idata, 0, clength); }  
    response.setContentType(con.getContentType());   
    BufferedReader rd = new BufferedReader(new InputStreamReader(con.getInputStream()));  String line; 
    while ((line = rd.readLine()) != null) { out.println(line); } 
    rd.close();        } 
    catch(Exception e) {       
    response.setStatus(500);       
    } %>

  10. #10
    e soapMessage che cos'è?
    I DON'T Double Click!

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.