2

こんにちは、現在、users.Am を認証するために xml ドキュメントを解析する必要があるアプリケーションに取り組んでいます。java.net.* パッケージの URLConnection クラスを使用して、xml 形式で応答を返す特定の URL として接続しています。jdom を使用してドキュメントを解析しようとすると、次のエラーが発生します。
org.jdom2.input.JDOMParseException: Error on line 1: Premature end of file

誰でも問題を特定して、解決策を教えてもらえますか? ありがとう、ここに私のコードのセクションがあります

try {
  String ivyString = "http://kabugi.hereiam.com/?username=" + ivyUsername + "&password=" + ivyPassword;

  URL authenticateURL = new URL(ivyString);
  URLConnection ivyConnection = authenticateURL.openConnection();
  HttpURLConnection ivyHttp = (HttpURLConnection) ivyConnection;
  System.out.println("Response code ==>" + ivyHttp.getResponseCode());
  if (ivyHttp.getResponseCode() != 200) {
    ctx.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Invalid username or password!", ""));
    page = "confirm.xhtml";
  } else {
    BufferedReader inputReader = new BufferedReader(new InputStreamReader(ivyConnection.getInputStream()));
    String inline = "";
    while ((inline = inputReader.readLine()) != null) {
      System.out.println(inline);
    }
    SAXBuilder builder = new SAXBuilder();

    Document document = (Document) builder.build(ivyConnection.getInputStream());
    Element rootNode = document.getRootElement();
    List list = rootNode.getChildren("data");
    for (int i = 0; i < list.size(); i++) {
      Element node = (Element) list.get(i);
      System.out.println("Element data ==>" + node.getChildText("username"));
      System.out.println("Element data ==>" + node.getChildText("password"));

    }

    page = "home.xhtml";
  }
} catch (Exception ex) {
  ex.printStackTrace();
  // ctx.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Invalid username or password!", ""));
}
4

3 に答える 3

4

入力ストリームを 2 回読み取っているためのようです。一度それを印刷し、次にドキュメントを作成します。Document オブジェクトを作成する時点では、入力ストリームはすでに完全に読み取られており、最後まで読み取られています。ストリームを一度だけ読み取る次のコードを試してください

        BufferedReader inputReader = new BufferedReader(new InputStreamReader(ivyConnection.getInputStream()));
        StringBuilder sb = new StringBuilder();
        String inline = "";
        while ((inline = inputReader.readLine()) != null) {
          sb.append(inline);
        }

        System.out.println(sb.toString());
        SAXBuilder builder = new SAXBuilder();

        Document document = (Document) builder.build(new ByteArrayInputStream(sb.toString().getBytes()));
于 2012-09-18T01:52:17.573 に答える
1

私は以前にこれに似た問題を抱えていました。圧縮されていない HTTP 接続の場合は、Wireshark を使用してパケットをトレースできます。おそらく、XML 応答データの先頭に予期しない XML BOM ヘッダー (その他の問題) があることがわかります。これは、たとえば、使用している HTTP ライブラリが http チャンクをサポートしていない場合や、xml のエンコーディングが間違っている場合に発生する可能性があります。

パケット スニファを使用してトラフィックを分析し、BOM ヘッダー (または BOM ヘッダーの欠落) を特定するまで、それはわかりません。いずれにせよ、問題が発生した場合は、ストリームをハッキングして BOM ヘッダーを確認できます。

于 2012-09-17T20:29:00.330 に答える
0

sax parse を使用すると、inputstream.

SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
ParseCustomer parseEventsHandler=new ParseCustomer();
sp.parse(ivyHttp.getInputStream(),parseEventsHandler);

以下ParseCustomerは、xml を読み取る解析クラスです。与えられたサンプルは次のとおりです。

class ParseCustomer extends DefaultHandler{
    @Override
    public void startDocument() throws SAXException {

        super.startDocument();
    }
    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {

        super.startElement(uri, localName, qName, attributes);//enter code here
        if (qName.equals("frameit")) {
            // compare element name and get value/ for further help read saxparser
            orderId=atts.getValue("orderId");
        }
        System.out.println(qName);

    }

    @Override
    public void endElement(String uri, String localName, String qName)
        throws SAXException {

        super.endElement(uri, localName, qName);
    }

    @Override
    public void characters(char[] ch, int start, int length)
        throws SAXException {

        super.characters(ch, start, length);
    }

}
于 2014-06-11T07:11:22.423 に答える