2013-09-28 4 views
2

일부 HTML 태그를 나타내는 XML 문자열이 있습니다. 일부 XML을 선택하여 HTML로 가져 오려고합니다.XML을 HTML로 가져 오기

내가 현재 뭘하는지의 예 : 코드 페이지의 끝 부분에 링크를 추가해야하지만, 난 단지 연결하지의 텍스트를 볼 수 있습니다

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

</head> 
<body> 
<div><p>som text</p></div> 
<h1>hi there</h1> 
<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> 

. HTML 태그는 작동하지 않습니다.

https://github.com/jindw/xmldom과 같은 jQuery 대신 다른 라이브러리를 사용했지만 그 결과는 같습니다. 업데이트 : 이것은 jsfiddle 링크입니다. http://jsfiddle.net/V4gHz/

+0

.html() XML 문서로 작업하지 않습니다. 내부 innerHTML 함수를 사용하기 때문입니다. –

+0

'.text()'는 어떨까요? – Jashwant

+0

nope. 전혀 작동하지 않습니다 –

답변

1

마침내 나는 내 자신의 질문에 대한 답을 찾았습니다. 것은 innerHTML 또는 XMLSerializer로 시간 낭비하지 않습니다. 그들은 모든 브라우저에서 작동하지 않기 때문에 파이어 폭스와 호환됩니다. $ .contents() 또는 $ .HTML()과 같은 jquery 솔루션도 firefox에서만 작동하며 오페라, 크롬 및 IE에서는 작동하지 않습니다.

'htmlize'라는 jquery 플러그인을 발견했습니다. wiche는 실제 시리얼 라이저입니다. 플러그인 주소는 다음과 같습니다. https://github.com/ZeeAgency/jquery.htmlize

github에서 추가 정보를 찾을 수 있습니다. 이것은 내 문제가 해결 된 방법입니다.

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

</head> 
<body> 
    <div><p>som text</p></div> 
    <h1>hi there</h1> 
    <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>