www.webdeveloper.com
Results 1 to 3 of 3

Thread: Convert String to XML

Hybrid View

  1. #1
    Join Date
    Dec 2013
    Posts
    6

    Convert String to XML

    Hi All,
    I'm using an web application where I can put HTTP post request. I have configured this part and the weird situation is that im getting a string back in stead of XML. When i send a request i give the following encoding: application/x-www-form-urlencoded; charset=UTF-8

    The response is look like:

    Code:
    <?xml version="1.0" encoding="utf-8"?>
    <string xmlns="http://URL">&lt;answer xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt;&lt;request&gt;&amp;lt;message&amp;gt;&amp;lt;authentication&amp;gt;&amp;lt;name&amp;gt;user&amp;lt;/name&amp;gt;&amp;lt;pass&amp;gt;XXXX&amp;lt;/pass&amp;gt;&amp;lt;sort&amp;gt;SB-RDW-BASIC&amp;lt;/sort&amp;gt;&amp;lt;/authentication&amp;gt;&amp;lt;parameters&amp;gt;&amp;lt;kent&amp;gt;93NRSN&amp;lt;/kent&amp;gt;&amp;lt;/parameters&amp;gt;&amp;lt;/message&amp;gt;&lt;/request&gt;&lt;result&gt;&lt;code&gt;00&lt;/code&gt;&lt;description&gt;Ok&lt;/description&gt;
    
    .....and so on
    I have put the whole reponse in a text field so i can process it. I have replaced all the weird characters by doing:

    Code:
    var test = "reference to the response text field";
    test = test.replace(/&apos;/g, "'");
    test = test.replace(/&quot;/g, '"');
    test = test.replace(/&lt;/g, '<');
    test = test.replace(/&amp;/g, '&');
    test = test.replace(/&gt;/g, '>');
    test = test.replace(/&lt;/g, '<');
    
    return test;
    In my web application there is a method to parse the XML and also a function to do xPath. But i couldt get it work, because:

    - When trying to work with xPath i get: .SAXParseException; lineNumber: 1; columnNumber: 1; Content is not allowed in prolog.
    - When I open the field (after the character replaces) in the browser i see the XML starting with the following:

    Code:
    <?xml version=\u00221.0\u0022 encoding=\u0022utf-8\u0022?>\n<string xmlns=\u0022URL\u0022>
    
    and so on...
    As you can see there there is also a \u0022 character (single quotes?), i dont know how to handle these....

    What is the best situation to parse the XML? Within the applicatoin there is a XMLParse function, but when i put the string (after the replaces) i couldnt get it parsed, i think the string is messed up or something? Anybody idea?

  2. #2
    Join Date
    Jun 2009
    Location
    Miami
    Posts
    118
    First off, you don't appear even to have well-formed, let alone valid, XML markup there.

    Your other problem appears to be that you have character encodings set improperly.

    You don't describe clearly how you are passing the XML content from client to server or from server to client.

    If you create HTML files, you should give a <meta> element that specifies the character encoding and then make sure the file is saved as that encoding for serving it from the server. Similarly if you prepare XML markup from form data with Javascript, you should encode properly any characters outside the typical ASCII format.

    On the server side, it should transmitting to you well-formed and VALID XML. Those byte codes (\u0022) are what you might use to prepare a URI for clean transmission, but they are not for XML.

    If you transmit server->client and client->server valid XML, you should be able to immediately use DOM manipulating functions to access and traverse nodes using DOM (including XPath) functions. On the client side, you can use DOMParser on received XML within Javascript. On the server side, PHP also has an object for manipulating valid XML received from the client.

    In fact, I make it a point NEVER to use the string/text such as the .responseText property of the XML HTTP request object when I know the object sent is valid XML. I ONLY use the .responseXML property, because if that property is NOT presented to me (that is, its value is null or undefined), it is NOT valid XML. If you get the file not using the XML-HTTP request object, you can see if it builds using the DOMParser constructor: if it does not, it is NOT valid XML.

    You have an error in coding of your web application if it is generating encoded bytes in your XML markup. You should address that first.

    You should also review the code generating all your XML markup and make sure it validates. You will have to learn how to make DTDs at a minimum: don't worry, they're easy to learn, and if you don't want to learn, you can buy or probably get for free programs which will look at your WELL-FORMED but NOT valid XML markup and generate a DTD for that particular markup (just search "DTD generator"). I recommend that you learn how to make XML Schema instead of DTDs if you want to be even more careful about the validity of your XML markup. (there are probably XML schema generators you can buy or get for free too, but using these generators might take away the fun from learning)

    On your HTTP client, for testing/debugging purposes, you can use the native developer tools in Chrome and latest IE version, but I still use Firebug extension in Firefox. Under the Net tab, you can see whether XML objects were parsed as valid, since it shows an XML tab in HTTP responses if they are valid.

    Post the relevant parts of your code to see why you are encoding quote characters and getting \uXXXX in the output. Make sure that your HTTP headers have the proper Content-Type header set. Make sure that client encodings (<meta>, <form> elements) are set properly. If you are using XML-HTTP Request Object, you should set the request header "Content-type" too. Make sure that when you save PHP and HTML files, it is with the proper encoding. I recommend UTF-8 without the byte offset marker (BOM) in everything you do. All editor applications (Notepad, I use RJ Text Editor!) have the easy ability to save in UTF-8 without BOM.

    Give us a look at how you are passing data back and forth between server and client and back.

  3. #3
    Join Date
    Dec 2013
    Posts
    6
    Hi Mavigozler,
    Thanks for your clear and extensive reply. I will try and debug the stuff. Thanks a lot!

    PS: Greatings to Ankara!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles