2

DOM を使用して xml ファイルを変更しようとしたところ、次のことが起こりました。

javax.xml.transform.TransformerException: java.io.FileNotFoundException: file:\D:\myproject\build\web\xml\myFile.xml (The filename, directory name, or volume label syntax is incorrect)
        at org.apache.xalan.transformer.TransformerIdentityImpl.createResultContentHandler(TransformerIdentityImpl.java:263)
        at org.apache.xalan.transformer.TransformerIdentityImpl.transform(TransformerIdentityImpl.java:296)
        at utils.UpdateUtils.BookUpdate(UpdateUtils.java:36)

これは私のコードです

        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();

        Document doc = db.parse(f);
        searchAndModify(doc); //modify xml's contents

        Source source = new DOMSource(doc);
        Result result = new StreamResult(f);
        TransformerFactory tff = TransformerFactory.newInstance();
        Transformer trans = tff.newTransformer();
        trans.transform(source, result);

f は、生成された私の xml です。Document doc にうまく解析されました。ただし、変換すると例外がスローされました。

新しいxml、同じフォルダーに解析しようとしましたが、役に立ちませんでした:

Result result = new StreamResult(new File(path, "newFile.xml"));

javax.xml.transform.TransformerException: java.io.FileNotFoundException: file:\D:\myProject\build\web\newFile.xml (The filename, directory name, or volume label syntax is incorrect)

誰かがこの問題に遭遇したか、解決策を持っていますか? 私を助けてください!

4

4 に答える 4

3

StreamResult result = new StreamResult(new File(filepath).getAbsolutePath());

のために働いていfile not found exceptionます。

于 2015-03-12T05:41:52.333 に答える
1

問題はここにあるようです:

java.io.FileNotFoundException: file:\D:\myProject\build\web\newFile.xml

あなたのファイル名は 6 文字で始まるように見えますfile:\。ファイル名が で始まることを確認してくださいD:

/ファイル名よりも URL を使用したい場合は、すべてのプラットフォームでURL にスラッシュ ( ) を使用する必要があるため、上記は有効な URL ではないことに注意してください。

于 2012-11-24T21:06:40.037 に答える
0

xml ファイルの正しいファイル パスを指定する必要があります。Web アプリケーション内のファイルにアクセスしようとしているようです。そのため、web.xml でアプリケーション リソース パスを指定できます。

<context-param>
  <param-name>xmlPath</param-name>
  <param-value>D:\somefolder\</param-value>
</context-param>

次に使用します

String xmlFilePath = new File(e.getServletContext().getInitParameter("xmlPath"));
File customerDataFile = new File(xmlFilePath , "newFile.xml");
于 2012-11-24T18:18:17.797 に答える
0

私はこの同じ問題に遭遇し、それが何であるかを知っていると信じています。

私にとっては:

  // Make a string stream out of the xml.
  Source source = new StreamSource(new ByteArrayInputStream(xml.toString().getBytes("UTF-8")));
  FileOutputStream stream = new FileOutputStream(file);
  StreamResult result = new StreamResult(stream);
  transformer.transform(source, result);
  stream.close();

うまくいきました

次に、これを磨きました

  // Make a string stream out of the xml.
  Source source = new StreamSource(new ByteArrayInputStream(xml.toString().getBytes("UTF-8")));
  // Transform it straight into the output file.
  StreamResult result = new StreamResult(file);
  transformer.transform(source, result);

私の開発マシンでもうまくいきました。

残念ながら、これを現場に設置したときに壊れました。xalanオンサイトのバージョンが私のテスト セットアップよりも古いことが判明しました。の古いバージョンでxalanは、s に直接書き込む際に問題があったようFileです。

@VGR のアイデアは近かったようです。

注意すべき微妙な点は、file:\D:\...が実際にはfile:/D:/...を読み取る必要があることです。これは xalan の以前のバージョンの問題です。

于 2014-07-16T11:23:10.003 に答える