Results 1 to 3 of 3

Thread: Convert String to XML

  1. #1
    Join Date
    Dec 2013

    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:

    <?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:

    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:

    <?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
    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
    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



X vBulletin 4.2.2 Debug Information

  • Page Generation 0.08906 seconds
  • Memory Usage 2,858KB
  • Queries Executed 15 (?)
More Information
Template Usage (33):
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (3)bbcode_code
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (3)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (3)postbit
  • (3)postbit_onlinestatus
  • (3)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (72):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_postinfo_query
  • fetch_postinfo
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates