$ .parseXML无法解析带有脚本标签的XML [英] $.parseXML cannot parse XML with script tag inside

查看:276
本文介绍了$ .parseXML无法解析带有脚本标签的XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用$.parseXML解析以下BPMN 2 XML:

I'm trying to parse the following BPMN 2 XML with $.parseXML:

<?xml version="1.0" encoding="UTF-8"?>
<definitions id="Definition"
         xmlns:tns="http://www.jboss.org/drools">
    <process processType="Private"
         isExecutable="true"
         id="com.sample.bpmn"
         name="Sample Process"
         tns:packageName="defaultPackage" >
        <scriptTask id="_2" name="Sample Script" scriptFormat="http://www.java.com/java">
            <script>person.id</script>
        </scriptTask>
    </process>
</definitions>

,但返回以下内容:

Uncaught Error: Invalid XML: 
<?xml version="1.0" encoding="UTF-8"?>
<definitions id="Definition"
             xmlns:tns="http://www.jboss.org/drools">
    <process processType="Private"
             isExecutable="true"
             id="com.sample....<omitted>...id 

JSFiddle

编辑

稍后在代码中,我必须找到XML的特定部分,例如过程,定义等:

Later in the code I have to find specific parts of the XML like process, definitions etc.:

xmlDoc   = $.parseXML(data),
$xml     = $(xmlDoc),
$def     = $xml.find('definitions'),
$process = $def.find('process'),

推荐答案

 var scriptBody = $('script#scriptBody').text();
 console.log(scriptBody);

 var parser = new DOMParser(); var xml =
 parser.parseFromString(scriptBody, 'text/xml'); console.log(xml);

做得很好.奇怪的是,当然jquery使用此实现(不适用于IE)

work fine. Strange, course jquery use this implementation ( not for IE )

在调试$函数上

parseXML: function( data ) {
    var xml, tmp;
    if ( !data || typeof data !== "string" ) {
        return null;
    }
    try {
        if ( window.DOMParser ) { // Standard
            tmp = new DOMParser();
            xml = tmp.parseFromString( data , "text/xml" );
        } else { // IE
            xml = new ActiveXObject( "Microsoft.XMLDOM" );
            xml.async = "false";
            xml.loadXML( data );
        }
    } catch( e ) {
        xml = undefined;
    }
    if ( !xml || !xml.documentElement || xml.getElementsByTagName( "parsererror" ).length ) {
        jQuery.error( "Invalid XML: " + data );
    }
    return xml;
},

xml.getElementsByTagName("parsererror")中的问题

problems in xml.getElementsByTagName( "parsererror" )

此页面包含以下错误:第2行第6列的错误: 仅在文档开始处才允许XML声明下面是一个 页面呈现到第一个错误为止."

"This page contains the following errors:error on line 2 at column 6: XML declaration allowed only at the start of the document Below is a rendering of the page up to the first error."

如果我删除了xml声明

if i remove xml declaration i get

此页面包含以下错误:列第10行的错误 29:文档末尾的额外内容

"This page contains the following errors:error on line 10 at column 29: Extra content at the end of the document

这篇关于$ .parseXML无法解析带有脚本标签的XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆