简体   繁体   English

JSON 到 xml 上的特殊字符

[英]special characters on JSON to xml

I'm working with Javascript and PHP.我正在使用 Javascript 和 PHP。

I jave a JSON like this:我有一个这样的 JSON:

[{"id": 32, "label": "Some Title", "type": "tinymce", "value": "<!DOCTYPE html>↵<html>↵<head>↵</head>↵<body>↵<p>asdasdasda&nbsp;30-09-2017</p>↵</body>↵</html>"}]

The real JSON is a loong array of objects/arrays.真正的 JSON 是一个长长的对象/数组数组。 I'm creating an XML file (with .xls extension) that will be read is Excel.我正在创建一个 XML 文件(扩展名为 .xls),它将被读取为 Excel。 With normal characters everything works correct, but I can't put those special characters in order to get Excel reading that XML file.使用普通字符,一切正常,但我不能放置这些特殊字符以使 Excel 读取该 XML 文件。 What can I do?我能做什么?

What can not do is:不能做的是:

  • Using another format: I need to read the XML with and .xls extension.使用另一种格式:我需要阅读带有 .xls 扩展名的 XML。
  • Change the data from the JSON, it's external.更改来自 JSON 的数据,它是外部的。

In the way I'm doing i, tThe XML results in something like this:在我做的方式中,tThe XML 结果如下:

<ss:Row>
  <ss:Cell>
    <ss:Data ss:Type="String"></ss:Data>
  </ss:Cell>
  <ss:Cell>
    <ss:Data ss:Type="String"></ss:Data>
  </ss:Cell>
  <ss:Cell>
    <ss:Data ss:Type="String"></ss:Data>
  </ss:Cell>
  <ss:Cell>
    <ss:Data ss:Type="String">Objeto del contrato</ss:Data>
  </ss:Cell>
  <ss:Cell>
    <ss:Data ss:Type="String"><!DOCTYPE html> // I don't need all the html tags, I just need to put the text of the <p> tags.
<html>
<head>
</head>
<body>
<p>asdasdasda&nbsp;30-09-2017</p>
</body>
</html></ss:Data>//here ends the wrong text coming from the JSON
  </ss:Cell>
</ss:Row>

which is not correct for Excel.这对 Excel 来说是不正确的。

Since the contents of the value attribute are HTML, you may have a hidden element (or even an element not attached to the DOM), set its innerHTML to the contents of this attribute, and then use your favorite DOM-manipulation library (jQuery for instance) to read the contents of the p attribute.由于value属性的内容是 HTML,你可能有一个隐藏元素(甚至一个没有附加到 DOM 的元素),将它的innerHTML设置为这个属性的内容,然后使用你喜欢的 DOM 操作库(jQuery for实例)读取p属性的内容。

For instance, using jQuery:例如,使用 jQuery:

var text = $('<div/>').html(o.value).find('p').text()

Alternatively, you could use a regex to match just then contents between <p> and </p> , though this may be more fragile if the format varies somewhat.或者,您可以使用正则表达式来匹配<p></p>之间的内容,尽管如果格式有所不同,这可能会更加脆弱。 In that case, you'll also need to decode HTML entities, which often involves doing much of the same as above.在这种情况下,您还需要解码 HTML 实体,这通常涉及执行与上述相同的大部分工作。

Given that you've mentioned in the question that you are using PHP, I'm going to assume that you are processing the JSON in PHP to create the XML file.鉴于您在问题中提到您使用的是 PHP,我将假设您正在 PHP 中处理 JSON 以创建 XML 文件。

In that case, you can use DOMXPath to query the exact element from the HTML value:在这种情况下,您可以使用DOMXPath从 HTML 值中查询确切的元素:

$html = "<!DOCTYPE html>
<html>
<head>
</head>
<body>
<p>asdasdasda&nbsp;30-09-2017</p>
</body>
</html>";

$doc = new DOMDocument;
$doc->loadHtml($html);
$xpath = new DOMXPath($doc);
$query = '/html/body/p';
$entries = $xpath->query($query);

print $entries->item(0)->nodeValue; //outputs 'asdasdasda 30-09-2017'

This assumes that the structure of the HTML will always be the same;这假定 HTML 的结构将始终相同; if that changes you would need to update your $query variable.如果发生变化,您需要更新您的$query变量。 For example, in order to select all p tags in the document, the XPath query would be //p .例如,为了选择文档中的所有p标签,XPath 查询将是//p

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM