如何将非UTF-8格式的xml文件转换为与UTF-8兼容的xml

问题描述

| 我有一个巨大的xml文件,其示例数据如下:
 <vendor name=\"aglaia\"><br>
              <vendorOUI oui=\"000B91\" description=\"Aglaia Gesellschaft für Bildverarbeitung ud Kommunikation m\" /><br>
         </vendor><br>
         <vendor name=\"ag\"><br>
              <vendorOUI oui=\"0024A9\" description=\"Ag leader Technology\" /><br>
         </vendor><br>
可以看到有文本“ GesellschaftfürBildverarbeitung”,该文本不兼容UTF-8,因为我从xml验证程序中收到错误错误如下: 导入失败: com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException:1字节UTF-8序列的无效字节1。 因此,查询是如何在Linux环境中解决此问题,以将xml文件转换为UTF-8兼容格式?还是在bash中有一种方法,使得在首先创建XML时我可以确保所有变量/字符串都以UTF-8兼容格式存储?     

解决方法

        使用字符集转换工具:
iconv -f ISO-8859-1 -t UTF-8 filename.txt
见gnu页 ...并且在文件http://standards.ieee.org/develop/regauth/oui/oui.txt中,“ aglia \”(如上述示例所示)报告为:
00-0B-91   (hex)            Aglaia Gesellschaft für Bildverarbeitung und Kommunikation m
000B91     (base 16)        Aglaia Gesellschaft für Bildverarbeitung und Kommunikation m
                            Tiniusstr. 12-15
                            Berlin  D-13089
                            GERMANY
似乎“ü\”是被曼格尔德化的角色。 更新资料 使用wget下载\“ oui.txt \”时,我在文件中看到字符“ \”。如果没有,则说明下载文件有问题。考虑使用以下方法之一:
wget --header=\'Accept-Charset: utf-8\'
尝试改用
curl -o oui.txt
如果以上方法均无效,只需在您喜欢的浏览器中打开链接并执行“另存为”即可。在这种情况下,请在下面的脚本中注释“ 5”行。 我成功使用以下脚本(更新BEGIN&END以获取有效的XML文件)
#!/bin/bash

wget http://standards.ieee.org/develop/regauth/oui/oui.txt
iconv -f iso-8859-15 -t utf-8 oui.txt > converted

awk \'BEGIN {
         print \"HTML-header\"
     }

     /base 16/ {
         printf(\"<vendor name=\\\"%s\\\">\\n\",$4)
         read
         desc = substr($0,index($0,$4))
         printf(\"<vendorOUI oui=\\\"%s\\\" description=\\\"%s\\\"/>\\n\",$1,desc)
     }
     END {
         print \"HTML-footer\"
    }
    \' converted
希望这可以帮助!