当前位置: 首页 > 知识库问答 >
问题:

如何获取不带转义字符的xml?(javascript,角度)

严永丰
2023-03-14

我在javascript领域还是个新手。所以请容忍我。:-)

用户按下“保存客户订单”按钮(这将向用户pc下载xml文件):

orders.component.html:

... 
    <form (ngSubmit)="saveOrdersAsXml()" novalidate>
...
    <div class="col-sm-12 text-right">
        <button type="submit" class="btn btn-primary btn-lg" >Save Customer Orders</button> 
    </div>
...

问题是:如何在没有转义字符的情况下获取xml?:

orders.component.ts:

export class OrderComponent implements OnInit 
{
    ...

    private saveOrdersAsXml($event) 
    {
        this.orderService.GetOrdersAsXml(this.customerNumber)
            .subscribe(res => 
            {
                // res = "Response with status: 200 OK for URL: http://localhost:52511/..."
                // res._body = "<?xml version=\"1.0\" encoding=\"utf-8\"?>..."      
                // _body contains the xml. How do I get the xml without escape characters??

                let xml: string = res;
                const file = new Blob([xml], { type: 'text/xml;charset=utf-8' }); 
                saveAs(file, this.customerNumber + '.xml'); // Currently the file will contain "Response with status: 200 OK for URL: http://localhost:52511/..." - I need it to be xml.
            }, error => this.errorMessage = <any>error);
    }
}

订单.服务. ts:

@Injectable()
export class OrderService 
{
    ...

    public GetOrdersAsXml(customerNumber: string): Observable<any> 
    {
        const dataResult = this._http.get("http://localhost:52511/api/orders/getordersasxml/" + customerNumber, , this.authenticationService.jwt()))
            .map((response: Response) => response)
            .catch(this.handleError);
        return dataResult;
    }
}

订单控制器.cs:

[Produces("application/json")]
[Route("api/orders")]
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
public class OrdersController : Controller
{
    ...

    [HttpGet("getordersasxml/{customerNumber}")]
    public string GetOrdersAsXml(string customerNumber)
    {
        // Here the xml is fine (e.g.: "<?xml version="1.0" encoding="UTF-8"?>...")
        return _orderBL.GetOrdersAsXml(customerNumber);
    }
}

编辑:工作了5个小时后,我还是不明白。

我尝试返回JObject、HttpResponseMessage、byte[]和xDocument。这越来越令人沮丧。。。

当我像这样使用xDocument时:

[HttpGet("getordersasxml/{customerNumber}")]
public XDocument GetOrdersAsXml(string customerNumber)
{
    // Here the xml is fine (e.g.: "<?xml version="1.0" encoding="UTF-8"?>...")
    var xml = _orderBL.GetOrdersAsXml(customerNumber);
    return XDocument.Parse(xml);
}

在前端,我这样做:

private saveOrdersAsXml($event) 
{
    this.orderService.GetOrdersAsXml(this.customerNumber)
        .subscribe(res => 
        {
            console.log("1: " + xmlResult);
            console.log("2: " + xmlResult._body);
            console.log("3: " + new XMLSerializer().serializeToString(xmlResult._body));

            ...
        }, error => this.errorMessage = <any>error);
}

控制台返回以下内容:

1:状态为:200 OK 的响应...

2:{"? xml":{"@版本":"1.0","@编码":"utf-16"},"客户"…

错误类型错误:未能对“XMLSerializer”执行“serializeToString”:参数1不属于“Node”类型。位于SafeSubscriber . search service . getxml file . subscribe . _ this . error message[as _ next](license . component . ts:111)位于safe subscriber。__tryOrUnsub (Subscriber.ts:254)位于safe Subscriber . next(Subscriber . ts:204)位于Subscriber。_ next(subscriber . ts:135)at subscriber . next(subscriber . ts:95)at catch subscriber . subscriber . _ next(subscriber . ts:135)at catch subscriber .subscriber . next(subscriber . ts:95)位于xmlhttprequest . onload(http . UMD . js:1259)位于zone delegate . invoke task(zone . js:425)位于object . oninvoke task(core . UMD . js:3913)

为什么要做这么简单的任务,比如将xml字符串从后端发送到前端?!

我现在要去看牙医,但稍后会回来。感谢您的帮助。

共有1个答案

翁建弼
2023-03-14

终于成功了。显然去看牙医有所帮助;-)

我是这样做的:

订单控制器.cs:

[HttpGet("getordersasxml/{customerNumber}")]
public string GetOrdersAsXml(string customerNumber)
{
    //return _orderBL.GetOrdersAsXml(customerNumber); // Gives trouble!

    // Put xml as a byte array into a json
    var xml = _orderBL.GetOrdersAsXml(customerNumber);
    var response = JsonConvert.SerializeObject(new
    {
        data = Encoding.UTF8.GetBytes(xml)
    }); 
    return response;
}

orders.component.ts:

private saveOrdersAsXml($event) 
{
    this.orderService.GetOrdersAsXml(this.customerNumber)
        .subscribe(res => 
        {
            // let xml: string = res; // Won't work

            // Parse json-string to json object and convert the data byte array to a string.
            let xml:string = atob(JSON.parse(res).data); 

            const file = new Blob([xml], { type: 'text/xml;charset=utf-8' }); 
            saveAs(file, this.customerNumber + '.xml'); 
        }, error => this.errorMessage = <any>error);
}

希望有人觉得这有帮助。:-)

 类似资料:
  • 问题内容: 远程服务器(不在我的控制范围内)发送一个JSON字符串,该字符串具有所有转义的字段名和值。例如,当我执行JSON.stringify(res)时,结果如下: 现在,当我执行alert(res.orderId)时,它说未定义。我认为是因为逃脱了。我该如何解决? 问题答案: 假设这 是显示 的实际值,请考虑: 请注意将JSON.stringify应用于值(这是一个 字符串 ,因为JSON

  • 我只想在Label1打个招呼

  • 问题内容: 如果您有一个带有unicode字符的字符串,则可以打印它,并获得未转义的版本: 但是如果我们有一个包含上面字符串的列表并打印出来: 您仍然会获得转义的字符序列。您如何才能使列表的内容不被转义,这可能吗?像这样: 另外,如果字符串是类型,那么您如何与上述相同? 问题答案: 打印字符串时,将获得对象方法的输出- 在这种情况下,该字符串不带引号。列表的方法不同,它创建一个包含打开和关闭的字符

  • 问题内容: 非常简单的问题,但是由于某种原因,在谷歌搜索10分钟后,我在任何地方都找不到答案。使用JavaScript打印时如何显示转义符? 例: 给出: 当我要它给: 问题答案: 如果你的目标是 并以字符串文字形式输出它包含的内容,您可以使用: 添加外引号(至少在Chrome的实现中),但是其中的内容是字符串文字(是的,这有点令人困惑)。 接受您提供的内容(在这种情况下为字符串),并返回包含该值

  • 问题内容: 我想将包含转义字符的字符串转换为普通格式,就像Python的词法分析器所做的一样: 当然,无聊的方法是将所有已知的转义字符一一替换:http : //docs.python.org/reference/lexical_analysis.html#string- literals 您将如何在上述代码中实现? 问题答案: 几个类似的编解码器是可用的,如ROT13和十六进制。 上面是Pyth

  • 我正在使用xslt转换xml文档。我需要使用输出转义来获得我的结果,因为我使用的工作服务器只会输出