【问题标题】:How to correct error: not well-formed (invalid token) at line 6如何纠正错误:第 6 行的格式不正确(无效令牌)
【发布时间】:2015-09-18 20:58:26
【问题描述】:

我正在尝试使用 perl 代码将 xml 文件转换为 html 文件,但收到此错误,即 xml 格式不正确且令牌无效。 代码:

#!/usr/bin/perl

use strict;
use warnings;
use XML::Parser;
use XML::LibXML;

my $parser = XML::Parser->new();
$parser->setHandlers(   Start   => \&start,
                        End     => \&end,
                        Char    => \&char,
                        Proc    => \&proc,
                );
my $header = &getXHTMLHeader();
print $header;
$parser->parsefile('output.xml');

my $currentTag = "";

sub start() {
        my ($parser, $name, %attr) = @_;
        $currentTag = lc($name);
        if ($currentTag eq 'doc') {
                print "<head><title>". "Output of snmpwalk for cpeIP4" . "</title></head>";
                print "<body><h2>" . "Output of snmpwalk for cpeIP4" . "</h2>";
                print '<table summary="' . "Output of snmpwalk for cpeIP4" . '"><tr><th>Tag Name</th><th>0</th><th>1</th><th>2</th><th>3</th><th>4</th><th>5</th><th>6</th><th>7</th><th>8</th><th>9</th><th>10</th><th>11</th><th>12</th><th>13</th><th>14</th><th>15</th><th>16</th></tr>';
        }
        elsif ($currentTag eq 'GI-eSTB-MIB-NPH') {
                print "<tr>";
        }
        #elsif ($currentTag !~ /^(0|1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16)$/)
        elsif ($currentTag ne '0' || $currentTag ne '1' || $currentTag ne '2' || $currentTag ne '3' || $currentTag ne '4' || $currentTag ne '5' || $currentTag ne '6' || $currentTag ne '7' || $currentTag ne '8' || $currentTag ne '9' || $currentTag ne '10' || $currentTag ne '11' || $currentTag ne '12' || $currentTag ne '13' || $currentTag ne '14' || $currentTag ne '15' || $currentTag ne '16' ) {
                print "<tr>";
        }
        else {
                print "<td>";
        }
}
sub end() {
        my ($parser, $name, %attr) = @_;
        $currentTag = lc($name);
        if ($currentTag eq 'doc') {
                print "</table></body></html>";
        }
        elsif ($currentTag eq 'GI-eSTB-MIB-NPH') {
                print "</tr>";
        }
        #elsif ($currentTag !~ /^(0|1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16)$/)
        elsif ($currentTag ne '0' || $currentTag ne '1' || $currentTag ne '2' || $currentTag ne '3' || $currentTag ne '4' || $currentTag ne '5' || $currentTag ne '6' || $currentTag ne '7' || $currentTag ne '8' || $currentTag ne '9' || $currentTag ne '10' || $currentTag ne '11' || $currentTag ne '12' || $currentTag ne '13' || $currentTag ne '14' || $currentTag ne '15' || $currentTag ne '16' ) {
                print "</tr>";
        }
        else {
                print "</td>";
        }
}
sub char() {
        my ($parser, $data) = @_;
        print $data;
}
sub proc() {
        my ($parser, $target, $data) = @_;
        if (lc($target) eq 'perl') {
                $data = eval($data);
                print $data;
        }
}
sub getXHTMLHeader() {
        my $header = '<?xml version="1.0" encoding="UTF-8"?>
        <!DOCTYPE html
          PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
          "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
          <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">';
        return $header;
}

output.xml 的内容是(例如,实际文件包含更多类似格式的详细信息):

<?xml version="1.0" encoding="UTF-8"?>

<doc>
    <GI-eSTB-MIB-NPH>
        <eSTBGeneralErrorCode>
            <0>INTEGER: 0</0>
        </eSTBGeneralErrorCode>
        <eSTBGeneralConnectedState>
            <0> INTEGER: true(1) </0>
        </eSTBGeneralConnectedState>
        <eSTBGeneralPlatformID>
            <0> INTEGER: 2076 </0>
        </eSTBGeneralPlatformID>
    </GI-eSTB-MIB-NPH>
</doc>

实际错误是:

not well-formed (invalid token) at line 6, column 13, byte 112 at /usr/lib64/perl5/XML/Parser.pm line 187

我已经搜索了解决方案并找到了一些提到相同问题的帖子,但没有找到可以帮助我的解决方案。以下是我从各种在线论坛中找到的尝试:

  1. 检查 xml 格式并在http://www.w3schools.com/xml/xml_server.asp 进行比较
  2. 添加空格 INTEGER: 0
  3. 已移除 - encoding="UTF-8"
  4. 将“UTF-8”更改为“UTF-16”和“ISO-8859-1”
  5. perl -ne 'print $_ if not m/^output_1.xml and used output_1.xml
  6. perl -ne'print $_ if ! /^
  7. 字符串 output.xml > output_1.xml 和使用的 output_1.xml

请建议我是否可以尝试更多。我不确定xml或我的代码是否真的有问题。

【问题讨论】:

  • 元素名称不能以数字开头。因此,0 是标记可以开始的&lt; 之后的无效标记。
  • 非常感谢 choroba。它为我省去了很多麻烦,我现在会重新考虑我的整个方法。
  • 如果你已经解决了你的问题,你能总结一下你所做的作为一个答案(并接受它)吗?
  • 我决定使用一个 xml 元素作为“eSTBGeneralErrorCode.0; eSTBGeneralConnectedState.0; etc”,而不是创建两个单独的元素。对于延迟接受答案,我深表歉意。不知何故,我看不到接受答案的选项。我认为这可能是由于帖子太旧或者我不需要访问它(这是我的第一个问题)。

标签: xml perl utf-8


【解决方案1】:

解决方案是遵循 choroba 的建议,避免元素名称以数字开头。

我决定使用一个 xml 元素作为 eSTBGeneralErrorCode.0; eSTBGeneralConnectedState.0; etc,而不是创建两个单独的元素。对于延迟接受答案,我深表歉意。

终于想通了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-27
    • 2021-05-08
    • 1970-01-01
    • 2016-05-04
    相关资源
    最近更新 更多