Linux 如何将非 UTF-8 格式的 xml 文件转换为符合 UTF-8 格式的 xml
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6336634/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert xml file which is in non UTF-8 format to xml that is UTF-8 compliant
提问by Nohsib
I have a huge xml file whose sample data is as follows :
我有一个巨大的 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>
as it can be see there are text " Gesellschaft f??r Bildverarbeitung " which is not UTF-8 compliant because which I am getting errors from the xml validator , errors like:
正如它所看到的,有文本“Gesellschaft f??r Bildverarbeitung”不符合 UTF-8,因为我从 xml 验证器收到错误,错误如下:
Import failed: com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException: Invalid byte 1 of 1-byte UTF-8 sequence.
So the query is how to take care of this in Linux environment to convert the xml file to UTF-8 compliant format? or is there a way in bash such that while creating the xml in the first place i can ensure that all variables/strings are stored in UTF-8 compliant format?
那么查询是如何在Linux环境中处理这个将xml文件转换为兼容UTF-8的格式?或者在 bash 中是否有一种方法可以在首先创建 xml 时确保所有变量/字符串都以符合 UTF-8 的格式存储?
采纳答案by Fredrik Pihl
Use the character set conversion tool:
使用字符集转换工具:
iconv -f ISO-8859-1 -t UTF-8 filename.txt
See gnu-page
参见gnu-page
...and in file http://standards.ieee.org/develop/regauth/oui/oui.txt"aglia" (as in your example above) is reported as:
...在文件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
it seems like "ü" is the character that gets mangeld.
似乎“ü”是被破坏的角色。
Update
更新
When downloading "oui.txt" using wget, I see the character "ü" in the file. If you don't have that something is broken in your download. consider using one of these:
使用 wget 下载“oui.txt”时,我在文件中看到字符“ü”。如果您没有,那么下载中的某些内容已损坏。考虑使用其中之一:
wget --header='Accept-Charset: utf-8'
- try using
curl -o oui.txt
instead
wget --header='Accept-Charset: utf-8'
- 尝试
curl -o oui.txt
改用
If none of the above works, just open the link in you favorite browser and do a "save as". In that case, comment the wget
line in the script below.
如果上述方法均无效,只需在您喜欢的浏览器中打开链接并“另存为”即可。在这种情况下,请注释wget
下面脚本中的行。
I had success with the following script (update BEGIN & END to get a valid XML-file)
我使用以下脚本取得了成功(更新 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", )
read
desc = substr(##代码##, index(##代码##, ))
printf("<vendorOUI oui=\"%s\" description=\"%s\"/>\n", , desc)
}
END {
print "HTML-footer"
}
' converted
Hope this helps!
希望这可以帮助!