私はXmlSlurperでGoogleの原子を解析しようとしています。私のユースケースは、このようなものです。XmlSlurperでUTF-8 xmlファイルを解析する
1)原子xmlをrestクライアントでサーバーに送信します。
2)リクエストを処理し、サーバー側で解析します。
サーバーをGroovyで開発し、XmlSlurperをパーサとして使用しました。しかし、私はsucced couldntと "コンテンツはプロローグで許可されていません"例外を取得します。そしてそれが起こった理由を見つけようとしました。原子xmlをutf-8でエンコードされたファイルに保存しました。そして、ファイルを読み込み、原子を解析しようとしましたが、同じ例外が発生します。しかし、その後、ansiでエンコードされたファイルにatom xmlを保存しました。そして、私は原子xmlを正常に解析しました。だから問題はXmlSlurperと "UTF-8"だと思う。
この制限についてお考えですか?私の原子xmlはutf-8でなければならないので、どうすればこの原子xmlを解析できますか?ご協力いただきありがとうございます。
XML:
<?xml version="1.0" encoding="UTF-8"?>
<entry xmlns:atom='http://www.w3.org/2005/Atom'
xmlns:gd='http://schemas.google.com/g/2005'>
<category scheme='http://schemas.google.com/g/2005#kind'
term='http://schemas.google.com/contact/2008#contact' />
<title type='text'>Elizabeth Bennet</title>
<content type='text'>Notes</content>
<gd:email rel='http://schemas.google.com/g/2005#work'
address='[email protected]' />
<gd:email rel='http://schemas.google.com/g/2005#home'
address='[email protected]' />
<gd:phoneNumber rel='http://schemas.google.com/g/2005#work'
primary='true'>
(206)555-1212
</gd:phoneNumber>
<gd:phoneNumber rel='http://schemas.google.com/g/2005#home'>
(206)555-1213
</gd:phoneNumber>
<gd:im address='[email protected]'
protocol='http://schemas.google.com/g/2005#GOOGLE_TALK'
rel='http://schemas.google.com/g/2005#home' />
<gd:postalAddress rel='http://schemas.google.com/g/2005#work'
primary='true'>
1600 Amphitheatre Pkwy Mountain View
</gd:postalAddress>
</entry>
読み取りファイルと解析:
BufferedReader br = new BufferedReader(
new InputStreamReader(new FileInputStream(file)));
while ((line = br.readLine()) !=null) {
sb.append(line);
}
プラットフォームのデフォルトエンコーディングでファイルを読んでいる:
String file = "C:\\Documents and Settings\\user\\Desktop\\create.xml";
String line = "";
StringBuilder sb = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
while ((line = br.readLine()) !=null) {
sb.append(line);
}
System.out.println("sb.toString() = " + sb.toString());
def xmlf = new XmlSlurper().parseText(sb.toString())
.declareNamespace(gContact:'http://schemas.google.com/contact/2008',
gd:'http://schemas.google.com/g/2005')
println xmlf.title
ラウンド長い道のりを行っていますか?どのようにしてXMLをどのように解析していますか?いくつかのコードは役に立ちます... –
私はnotsad ++でファイルを作成します。これはエンコーディングタイプansiです。私はコピー貼りを作った。 – erimerturk
失敗しているXMLの例もありますか? –