簡體   English   中英

在XML文件中將雙引號替換為“

[英]Replace double quote with " in XML file

我有一個包含引號的XML文件,如下所示

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

我需要更換xxxxx"yyyyxxxxx&quot;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; 修復第7行的SAXParseException。我無法將"替換&quot;

編輯:

我不能用這個答案 我正在尋找不同的解決方案,因為

  1. XML檔案較大(> 100MB)
  2. 因此,我認為按照答案的建議編譯和替換雙引號內的每一行都是不可行的。
  3. 我正在尋找像所有替換

content = content.replaceAll( "(&(?!amp;))", "&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;"));
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM