3

In the result of an AJAX call I have multiple fields in XML format. One of the nodes called Request contains an entire XML structure that I want to display.

var request = $(xml).find("Request").html()

The above line returns me the content of the Request node. However, one of the tags is called <INPUT>. Which is ofcourse being interpreted as an HTML-element. This is the result I get in my var request :

<root>
    <input>
        <i_date>20150316</i_date>
        <i_order_nr>110535</i_order_nr>
        <i_sequence_number>0057</i_sequence_number>
        <i_time>105016</i_time>
        <i_uom>KG</i_uom>

</root>

As you can see the closing </input> tag is missing. It is present in the response of my Ajax call. But by using JQuery .html() it is removed. How can I get the content of the Request node without Jquery interpretting the tags?

8
  • I would try some dirty hack like $(xml).find("Request").html().replace(/input/g , "input2"), so you get an <input2></input2> tag or something, which won't get interpreted. Comes with no warranties. Commented Apr 8, 2015 at 12:33
  • 2
    It is not the problem with .html() but with $(xml) itself. And it is time to use hacks like replacing <input> with <input2> or to use another technologies for parsing. Commented Apr 8, 2015 at 12:34
  • 1
    @JeremyThille 1. it will be better to replace <input> String, not just input. 2. It has to be done in the original string before $(xml) is executed. Commented Apr 8, 2015 at 12:36
  • If xml is a string, do a regex before doing $(xml) to replace all <input>, something like xml.replace(/<input\s+?([^>]+)?>/gi, '<item $1>') - untested Commented Apr 8, 2015 at 12:41
  • 2
    what is your objective? If you want to show that XML in plain text use .text instead of .html, if you want to get it parsed by the browser (it's odd because it's not HTML) you shouldn't fix it in client side, it's in invalid HTML, you should fix it at your source. Commented Apr 8, 2015 at 12:48

2 Answers 2

0

Try this: var response = $('<div/>').text($(xml).find("Request").html()).html(); It should create a div element add your xml as text to that div(which in order would escape html tags) and then the html() function gets you the escaped text.

Sign up to request clarification or add additional context in comments.

1 Comment

I tested this. It escaped the tags but the closing </input> tag is still missing
0

Thanks for all the comments. I eventually went with the dirty way. I don't like doing this stuff but it seems it is the only option.

xml= xml
     .replace(/<input>/g , "<input2>")
     .replace(/<\/input>/g , "<\/input2>");
var request = $(xml).find("Request").html()
              .replace(/<input2>/g , "<input>")
              .replace(/<\/input2>/g , "<\/input>");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.