11

XML 形式のコンテンツを持つ文字列を JDom ドキュメントに変換する方法。

私は以下のコードで試しています:

String docString = txtEditor.getDocumentProvider().getDocument(
txtEditor.getEditorInput()).get();

SAXBuilder sb= new SAXBuilder();

doc = sb.build(new StringReader(docString));

上記の問題を解決するのを手伝ってくれる人はいますか。前もって感謝します!!

4

2 に答える 2

18

This is how you generally parse an xml to Document

try {
  SAXBuilder builder = new SAXBuilder();
  Document anotherDocument = builder.build(new File("/some/directory/sample.xml"));
} catch(JDOMException e) {
  e.printStackTrace();
} catch(NullPointerException e) {
  e.printStackTrace();
}

This is taken from JDOM IBM Reference

In case you have string you can convert it to InputStream and then pass it

String exampleXML = "<your-xml-string>";
InputStream stream = new ByteArrayInputStream(exampleXML.getBytes("UTF-8"));
Document anotherDocument = builder.build(stream);

For the various arguments builder.build() supports you can go through the api docs

于 2013-03-05T08:59:16.243 に答える
9

これは、実際の FAQ よりもアクセスしやすい回答が必要な FAQ です:文字列からドキュメントを作成するにはどうすればよいですか?

だから、私は問題#111を作成しました

価値があるのは、この状況のエラーメッセージを以前に改善したことです(前の問題#63を参照してください。次のようなエラーが表示されるはずです:

MalformedURLException mx = new MalformedURLException(
    "SAXBuilder.build(String) expects the String to be " +
    "a systemID, but in this instance it appears to be " +
    "actual XML data.");

要するに、次のものを使用する必要があるということです。

Document parseddoc = new SaxBuilder().build(new StringReader(myxmlstring));

ロルフル

于 2013-03-05T11:37:18.537 に答える