javascript - AngularJS:将 XML 字符串加载到 XML 文档

标签 javascript xml angularjs domparser

我有 XML 字符串,我必须将其加载到 DOM 文档。 我已经尝试过这个:

    if ($window.DOMParser)
    {
        parser=new $window.DOMParser();
        xml=parser.parseFromString(data,"application/xml");
    }
    else // Internet Explorer
    {
        xml=new ActiveXObject("Microsoft.XMLDOM");
        xml.async=false;
        xml.loadXML(data); 
    }

但是在 Angular Controller.js 上它不起作用! 我得到了这个:

<body xmlns="http://www.w3.org/1999/xhtml"><parsererror style="display: block; white-space: pre; border: 2px solid #c77; padding: 0 1em 0 1em; margin: 1em; background-color: #fdd; color: black"><h3>This page contains the following errors:</h3><div style="font-family:monospace;font-size:12px">error on line 1 at column 1: Document is empty
    </div><h3>Below is a rendering of the page up to the first error.</h3></parsererror></body>

感谢您的提前。

最佳答案

我已经将 Angular 调用更改为 XML 调用,并且不必解析对文档的响应,因为他... 我的电话看起来像:

    $http({
            method  : 'GET',
            url     : "http://localhost:8080/getXML",
            timeout : 10000,
            params  : {
                fileName: fileName
            },
            transformResponse : function(data) {
                return $.parseXML(data);
            }
        }).success(function(data) {
           // data = document object
        });

我从 API 返回一个有效的 XML 字符串,并在 @RequestMapping 上的 Spring 上定义了返回类型以生成 = MediaType.APPLICATION_XML_VALUE。 希望它对您也有帮助。

关于javascript - AngularJS:将 XML 字符串加载到 XML 文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31648945/

相关文章:

xml - 缺少节点的 xpath

xml - XML填写PDF表格?

AngularJS:如何将 ui-grid 数据导出到 Excel?

javascript - 如果第 4 个父级 hasClass,则将类添加到 DIV

c# - 如何检查元素标签是否为结束元素

google-analytics - 使用 google analytics 处理带有散列的 url #

javascript - ng-model AngularJs 中的函数调用

javascript - 我无法调整固定位置

javascript - 类实例无法传递给包含 Jasmine 描述 block 和 it block 的函数

javascript - 如何在此 Angular 2 样本上使用 HTML5 模式?