【问题标题】:Replace double quote with " in XML file在 XML 文件中用 " 替换双引号
【发布时间】:2016-12-22 14:34:12
【问题描述】:

我有一个包含如下引号的 XML 文件

<feast key="NAME" value="NAME TEST 'xxxxx"yyyy' $"/>

我需要将xxxxx"yyyy 替换为xxxxx"yyyy

注意:xxxxx 和 yyyy 由用户定义。所以它可以是任何形式。

这里我包含了示例 XML 和要解析的代码。

TestSaxParse.xml

<?xml version="1.0" encoding="US-ASCII" ?> 
<TEST Office="TEST Office">
    <LINE key="112313133320">
        <TESTNO value="0"/>
        <FEATURE>
            <feast key="001" value="001"/>
            <feast key="NAME" value="NAME TEST 'xxxxx_&_yyyy' $"/>
        </FEATURE>
    </LINE>
    <LINE key="112313133321">
        <TESTNO value="0"/>
        <FEATURE>
            <feast key="002" value="002"/>
            <feast key="NAME" value="NAME TEST 'xxxxx"yyyy' $"/>
        </FEATURE>
    </LINE>
</TEST>

SaxParseEx.java

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class SaxParseEx extends DefaultHandler{

    private static String xmlFilePath = "/home/system/TestSAXParse.xml";

    public static void main(String[] args) {

        SaxParseEx SaxParseEx = new SaxParseEx();
        SAXParserFactory fact = SAXParserFactory.newInstance();
        SAXParser parser;
        try {

            Path path = Paths.get(xmlFilePath);
            Charset charset = StandardCharsets.UTF_8;
            String content = new String(Files.readAllBytes(path), charset);

            // replace & with &amp; 
            content = content.replaceAll( "(&(?!amp;))", "&amp;");
           // content = content.replaceAll( "(\"(?!quot;))", "&quot;"); Need regex to replace " with &quot; only on specific place where i mentioned above

            // Write updated content to XML file
            Files.write(path, content.getBytes(charset));

            // XML Parsing
            parser = fact.newSAXParser();
            parser.parse(new File(xmlFilePath), SaxParseEx);
            System.out.println("PARSE SUCCESS");
            return;
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("PARSE FAILED");
    }
}

O/P

org.xml.sax.SAXParseException; systemId: file:/home/system/TestSAXParse.xml; lineNumber: 14; columnNumber: 46; Element type "feast" must be followed by either attribute specifications, ">" or "/>".

我已将所有 &amp;amp; 替换为 &amp;amp; 以修复第 7 行上的 SAXParseException。我无法将 " 替换为 &amp;quot;

编辑:

我不能使用this answer。我正在寻找不同的解决方案,因为

  1. XML 文件过大(> 100MB)
  2. 所以我认为按照答案中的建议编译和替换双引号值内的每一行是不可行的。
  3. 我正在寻找全部替换

content = content.replaceAll( "(&amp;(?!amp;))", "&amp;amp;");

有没有可能写一个这样的正则表达式?

【问题讨论】:

  • 我认为,gt;lt; 之前的 &amp;amp; 也不应该被替换。必须是content.replaceAll( "&amp;(?!amp;|[gl]t;)", "&amp;amp;");
  • 因为您的 xml 从逻辑上无法解析。这里的问题是 'xxxxx" 结束了 xml 标签,你得到一个解析错误。
  • 刚刚制定了另一个正则表达式,可以帮助解决链接问题中的代码(稍作修改):="(.*?)"(?=\s+[\w:-]+="|\s*\/?&gt;)。但是,该解决方案应该有效。
  • @Wiktor Stribiżew:请查看我更新的问题。

标签: java regex xml saxparser


【解决方案1】:

' 包含 &amp;quot; 时,我将所有 " 替换为 &amp;quot;。所以我在Files.write之前添加了以下几行

Pattern pattern = Pattern.compile("'(.*[\"].*)'");
Matcher matcher = pattern.matcher(content);
while (matcher.find()) {
    content = content.replaceAll(matcher.group(1), matcher.group(1).replace("\"", "&quot;"));
}

【讨论】:

    猜你喜欢
    • 2013-06-13
    • 1970-01-01
    • 2013-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多