87371

import XML to HTML

Question:

I have a string of XML that represents some HTML tags. I want to select some of the XML and import it to HTML.

An example of what i'm currently doing:

<html> <head> <meta charset="utf-8"> <script src="http://code.jquery.com/jquery-1.9.1.js"></script> </head> <body> <div>

som text

</div>

hi there

<button>click</button> <script> xml = '<?xml version="1.0" encoding="utf-8"?><root><html><a href="#">the new link</a></html></root>'; $xml =$($.parseXML( xml )); $('button').on('click',function(){ var content = $xml.find('html:first').contents(); content.appendTo('body'); }); </script> </body> </html>

The code should append a link at the end of the page but I can only see text, its not link. No HTML tag works.

I used another library to do this instead of jQuery like <a href="https://github.com/jindw/xmldom" rel="nofollow">https://github.com/jindw/xmldom</a> , but the result is same. update : this is the jsfiddle link : <a href="http://jsfiddle.net/V4gHz/" rel="nofollow">http://jsfiddle.net/V4gHz/</a>

Answer1:

finally i find answer to my own question. the thing is dont wast your time with innerHTML or XMLSerializer . becuase they dont work for all browsers and they are just compatible with firefox. and jquery solutions like $.contents() or $.HTML() also work only for firefox and doesnt work for opera , chrome and IE.

i find a jquery plugin called 'htmlize' wiche is real serializer. the plugin address is : <a href="https://github.com/ZeeAgency/jquery.htmlize" rel="nofollow">https://github.com/ZeeAgency/jquery.htmlize</a>

you can find additional information from github. and this is how my problem solved :

<html> <head> <meta charset="utf-8"> <script src="http://code.jquery.com/jquery-1.9.1.js"></script> </head> <body> <div>

som text

</div>

hi there

<button>click</button> <script> xml = '<?xml version="1.0" encoding="utf-8"?><root><html><a href="#">the new link</a></html></root>'; $xml =$($.parseXML( xml )); $('button').on('click',function(){ var content = $xml.find('html:first').htmlize(); $(content).appendTo('body'); }); </script> </body> </html>

Recommend

  • Which MySQL collation compares e.g. é and e as equal?
  • Numpy - Finding matches across multiple co-ordinates
  • Creating a Countdown Watch with a slider in Xcode
  • getting cpu usage of one process using c++ (win32)
  • jinja2 template not found and internal server error
  • Splash Screen will not display
  • Xamarin Android | Layout style
  • Can't remove headers after they are sent
  • How to convert SOAP response with xsi values to json in WSO2esb
  • Floated image with variable width and heading with background image
  • How to get current document uri in XSLT?
  • saving file generated by TCPDF
  • JBoss External Properties Files in Classpath
  • onBackPressed() not being executed
  • Alternative To body {overflow:scroll;} That Will Prevent Page Jostling/Wriggling?
  • Why Encoding.ASCII != ASCIIEncoding.Default in C#?
  • Play WS (2.2.1): post/put large request
  • Bad request using file_get_contents for PUT request in PHP
  • Android fill_parent issue
  • Jetty Server not starting: Unable to establish loopback connection
  • FileReader+canvas image loading problem
  • Insert into database using onclick function
  • Deselecting radio buttons while keeping the View Model in synch
  • Encrypt data by using a public key in c# and decrypt data by using a private key in php
  • Why ng-show works with ng-repeat but ng-if doesn't? [duplicate]
  • Why HTML5 Canvas with a larger size stretch a drawn line?
  • SSO with signing and signature validation doesn't work
  • Deserializing XML into class C#
  • Change an a tag attribute in JavaScript based on screen width
  • jquery mobile loadPage not working
  • Properly structure and highlight a GtkPopoverMenu using PyGObject
  • Warning: Can't call setState (or forceUpdate) on an unmounted component
  • Is there a mandatory requirement to switch app.yaml?
  • Data Validation Drop Down Box Arrow Disappearing
  • How to include full .NET prerequisite for Wix Burn installer
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • How do you join a server to an Active Directory (domain)?
  • costura.fody for a dll that references another dll
  • How to Embed XSL into XML
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize