0

数値文字参照 (  など) を含む XML ドキュメントを解析するときに問題が発生します。私が直面している問題は、ドキュメントが解析されるときに & が & ; に置き換えられることです。(; の前にスペースを入れない) ため、解析されたドキュメントには & ;#xA0; が含まれます。これが起こらないようにするにはどうすればよいですか? を使用してみxmlDoc.setExpandEntityReferences(false)ましたが、何も変わらないようです。

ドキュメントを解析するための私のコードは次のとおりです。

public static Document getXmlDoc(File xmlFile) throws ParserConfigurationException, SAXExeption, IOException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setIgnoringElementContentWhitespace(true);
    factory.setExpandEntityReferences(false);
    DocumentBuilder builder = factory.newDocumentBuilder();
    return builder.parse(xmlFile);
}

どんな助けでも大歓迎です。

編集:

上記のコードから解析された XML は変更され、ファイルに書き戻されます。これを行うコードは以下のとおりです。

public static File saveXmlDoc(Document xmlDocument, String outputToDir, String outputFilename) throws IOException {
    String outputDir = outputToDir;
    if (!outputDir.endWith(File.separator)) outputDir += File.separator;
    if (!new FIle(outputDir).exists()) new File(outputDir).mkdir();
    File xmlFile = new File(outputDir + outputFilename);
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "no");
    StreamResult saveResult = new StreamResult(outputDir + outputFilename);
    DOMSource source = new DOMSource(xmlDocument);
    transformer.transform(source, saveResult);

    return xmlFile;
}

編集2:

の誤字を修正しましたfactory.setIgnoringElementContentWhitespace(true);

編集 3 - 私の解決策:

私の評判は低すぎて自分の質問に答えることができないため、これをすべて修正するために使用した解決策を次に示します。

この問題を解決するために変更した機能は次のとおりです。

XML ドキュメントを取得するには:

    public static Document getXmlDoc(File xmlFile) throws ParserConfigurationException, SAXException, IOException {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setIgnoringElementContentWhitespace(true);
        factory.setExpandEntityReferences(false);
        DocumentBuilder builder = factory.newDocumentBuilder();
        return builder.parse(xmlFile);
    }

XML ドキュメントを保存するには:

    public static File saveXmlDoc(Document xmlDocument, String outputToDir, String outputFilename) throws Exception {
        readNodesForHexConversion(xmlDocument.getChildNodes());
        String xml = getXmlAsString(xmlDocument);

        // write the xml out to a file
        Exception writeError = null;
        File xmlFile = null;
        FileOutputStream fos = null;
        try {
            if (!new File(outputToDir).exists()) new File(outputToDir).mkdir();
            xmlFile = new File(outputToDir + outputFilename);
            if (!xmlFile.exists()) xmlFile.createNewFile();
            fos = new FileOutputStream(xmlFile);

            byte[] xmlBytes = xml.getBytes("UTF-8");
            fos.write(xmlBytes);
            fos.flush();
        } catch (Exception ex) {
            ex.printStackTrace();
            writeError = ex;
        } finally {
            if (fos != null) fos.close();
            if (writeError != null) throw writeError;
        }

        return xmlFile;
    }

XML ドキュメントを文字列に変換するには:

        public static String getXmlAsString(Document xmlDocument) throws TransformerFactoryConfigurationError, TransformerException {
    DOMSource domSource = new DOMSource(xmlDocument);
    StringWriter writer = new StringWriter();
    StreamResult result = new StreamResult(writer);
    Transformer transformer;
    transformer = TransformerFactory.newInstance().newTransformer();
    transformer.transform(domSource, result);
    return writer.toString();
}
4

1 に答える 1

1

現時点では問題を再現できません。これは、次のことを試みる短いが完全なプログラムです。

import org.w3c.dom.*;
import java.io.*;
import javax.xml.*;
import javax.xml.parsers.*;

public class Test {
    public static void main (String[] args) throws Exception {
        byte[] xml = "<foo>&#xA0;</foo>".getBytes("UTF-8");
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setIgnoringElementContentWhitespace(true);
        factory.setExpandEntityReferences(false);
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document document = builder.parse(new ByteArrayInputStream(xml));
        Element element = document.getDocumentElement();
        String text = element.getFirstChild().getNodeValue();
        System.out.println(text.length()); // Prints 1
        System.out.println((int) text.charAt(0)); // Prints 160
    }
}

上記の XML が再び書き出されることは明らかではありません。それを行うために使用しているコードを示していただけると助かりますが、テキスト ノードの 1 文字の値が読み取られていないことは明らかです。アンパサンドの後に「#xA0;」が続きます。個別に、あなたの質問がそれを説明していると思うので、「 」と書かれているのを見ると本当に驚きます.

問題を実証する同様の短いが完全なプログラムを書くことができますか? これからも自分なりに頑張ります。

于 2012-11-02T18:04:08.473 に答える