简体   繁体   English

XML - JavaScript - >如何将XML标记显示为HTML

[英]XML - JavaScript -> How to display XML tags as HTML

The problem I have is that when an XML node has no value, the open and close tags are on different rows like this 我遇到的问题是,当XML节点没有值时,open和close标签位于不同的行上

<td ID="ref_4b73aa95ffd2289_phrPayer_1_DateEffective">
</td>
<td ID="ref_4b73aa95ffd2289_phrPayer_1_DateTerminated">
</td>

and when the node has data, the open and close tags are on one line: 当节点有数据时,open和close标签在一行上:

<td ID="ref_4b73ba85ffd2289_phrPayer_1_Company">Medicare Parts A&amp;B</td>

The problem I have is that when I read the data, it is read line by line. 我遇到的问题是,当我读取数据时,它是逐行读取的。 Following is the XML data that is used: 以下是使用的XML数据:

 <tr>
     <td ID="ref__phrPayer_1_Company">Medicare Parts A&amp;B</td>
     <td ID="ref__phrPayer_1_DateEffective">
     </td>
     <td ID="ref__phrPayer_1_DateTerminated">
     </td>
     <td ID="ref__phrPayer_1_PolicyNumber">
     </td>
     <td ID="ref__phrPayer_1_GroupNumber">
     </td>
     <td ID="ref__phrPayer_1_SubscriberName">AdamJulie</td>
 </tr>

* Above coding consistes of 6 columns, although as displayed above, there are 10 lines of coding. * 以上编码由6列组成,尽管如上所示,有10行编码。 That is the main problem. 这是主要问题。 * *

When I try to split up the data into an Array using the following : 当我尝试使用以下内容将数据拆分为Array

var purposeArray = purposeOfVisit.innerHTML.split("\n");

The data I receive is: 我收到的数据是:

Medicate Parts A&amp;B,,,,,,,,,AdamJulie

when it should be: 应该是什么时候:

Medicare Parts A&amp;B,,,,,AdamJulie

The length of the array is the same as the number of lines which is 10 in this case, where it should be 5. 数组的长度与在这种情况下为10的行数相同,其中应为5。

What I want to know is how I read the td tags with the data . 我想知道的是我如何用数据读取td标签

I want the innerHTML to read the folling code as an example: 我希望innerHTML以下面的代码作为示例来阅读:

<td ID="ref__phrPayer_1_Company">Medicare Parts A&amp;B</td>
<td ID="ref__phrPayer_1_DateEffective">
</td>
<td ID="ref__phrPayer_1_DateTerminated">
</td>
<td ID="ref__phrPayer_1_PolicyNumber">
</td>
<td ID="ref__phrPayer_1_GroupNumber">
</td>
<td ID="ref__phrPayer_1_SubscriberName">AdamJulie</td>

instead of: 代替:

Medicare Parts A&amp;B
.
.
.
.
.
.
.
.
AdamJulie

The . . Represents a null value. 表示空值。

Is this possible and if it is how would I do this? 这是可能的,如果是这样我会怎么做? or to replace the <td> tags with &lt;<td> and </td> tags with </td>&lt; 或替换<td>用标签&lt;<td></td>与标记</td>&lt; ?

Instead of using an XML parser in such a manner , you can read in an xml string via a stream like so: 您可以通过如下所示的流读取xml字符串, 而不是以这种方式使用XML解析器

var XMLString = "<tbody><tr><td ID=''>Medicare Parts A&amp;B</td><td ID=''></td><td ID=''></td><td ID=''></td><td ID=''></td><td ID=''>AdamJulie</td></tr><tr><td ID=''>Test2</td><td ID=''></td><td ID=''></td><td ID=''>123</td><td ID=''></td><td ID=''></td></tr></tbody>";

Where you can split the string after each column: 您可以在每列之后split字符串:

myXMLArray = XMLString .split("</td>");

And use a regular Expression to remove all tags. 并使用正则表达式删除所有标签。

var regex = /(<([^>]+)>)/ig;

for( var x = 0; x < XMLString .length; x++)
{
result[x] = XMLString [x].replace(regex, "");
}

Goodluck... 祝好运...

也许检查Sarissa图书馆。

The innerHTML method returns whatever lies inside the HTML-tag. innerHTML方法返回HTML标记内的任何内容。 If you want to fetch the HTML tag with the data, i suggest you use something like: 如果你想用数据获取HTML标签,我建议你使用类似的东西:

document.getElementsByTagName('td')

You are trying to write your own XML parser, and you are doing it wrong. 您正在尝试编写自己的XML解析器,并且您做错了。 You can't simply split XML at line boundaries and then parse each line separately. 您不能简单地在行边界处拆分XML,然后分别解析每一行。

There are plenty of good XML parsers available, and it's best to use one of them. 有很多好的XML解析器可供使用,最好使用其中一种。 If you must parse the XML yourself, then you need to study the art of parser-writing; 如果你必须自己解析XML,那么你需要学习解析器编写的艺术; it's not a job for amateurs. 这对业余爱好者来说不是一份工作。

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

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