簡體   English   中英

從Web服務檢索到的Java- XML

[英]Java- XML retrieved from a web service

好的,所以我想做的是創建一個Java程序,該程序使用從Web服務獲取的數據。 我可以獲取數據,但格式為XML文檔,當我將其打印到(Eclipse)控制台時,每個字母之間都有空格,而replaceAll方法不起作用。 代碼的相關部分如下。

BufferedReader reader = new BufferedReader(new InputStreamReader(new URL(webAdress).openStream()));
String XMLcode = reader.readLine();
XMLcode = XMLcode.concat(reader.readLine());
XMLcode = XMLcode.replaceAll(" ", "");
System.out.println(XMLcode); //in the finished program, I will do something with the data in the XML document.

結果看起來像這樣-

þÿ < ? x m l  v e r s i o n = " 1 . 0 " e n c o d i n g = " U T F - 8 " ? >

然后是我要尋找的實際數據。 一些互聯網搜索暗示þÿ表示這是一個文本編碼問題,但是我可以找到的將UTF-8轉換為UTF-16的所有方法都無濟於事(但是,確實可以將þÿ更改為??)。 是否有人知道如何解決此問題,或者是否知道讀取不需要文件中可用XML的“標准”方式?

注意:我沒有提出有關Web服務的問題,因此無法對其進行修改,也不知道它是如何工作的。

你為什么不嘗試

XMLcode = XMLcode.replaceAll(“ \\ s”,“”)

也許是一個好主意,嘗試:

new BufferedReader(new InputStreamReader(new URL(webAdress).openStream(), "UTF-16"));

您可以使用以下代碼對其進行測試:

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;

public class CharsetChanging {

    public static void main(final String[] args) throws IOException {

        File infile = new File("/tmp/utf16.txt");

        FileInputStream inputStream = new FileInputStream(infile);

        Reader in = new InputStreamReader(inputStream, "UTF-16");

        int read;

        while ((read = in.read()) != -1) {
            System.out.print(Character.toChars(read));
        }

        in.close();
    }
}

只需將new FileInputStream(infile)替換為new URL(webAdress).openStream()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM