JAVA代码片段,用于将整个XML文件中的单引号(')替换为双引号

JAVA code snippet to replace single quote(') to double quote in whole XML file

提问人:Kashyap Savsani 提问时间:2/19/2021 最后编辑:Kashyap Savsani 更新时间:2/21/2021 访问量:662

问:

我有一个带有嵌套标签的 XML 文件。我们可以使用 DOM、JDOM 解析器 我想将所有标签的字符串内从单引号(')替换为整个XML文件中的双引号。标签也可以嵌套在标签中。我想要一些 for 循环来查找所有标签和替换值,例如 HYPER SHIPPING'SDN BHD_First_Page --> HYPER SHIPPING''SDN BHD_First_Page

示例代码

    public void iterateChildNodes(org.jdom.Element parentNode) {
        if(parentNode.getChildren().size() == 0) {
            if(parentNode.getText().contains("'")) {
                parentNode.setText(parentNode.getText().replaceAll("'", "\'"));
                LOGGER.info("*************  Below Value updated");
                LOGGER.info(parentNode.getText());
            }
        }else {
            List<Element> rec = parentNode.getChildren();
            for(Element i : rec) {
                iterateChildNodes(i);
            }
        }
    }

示例 XML 文件

    <Document>
        <Identifier>DOC1</Identifier>
        <Type>HYPER SHIPPING SDN BHD</Type>
        <Description>HYPER SHIPPING SDN BHD</Description>
        <Confidence>33.12</Confidence>
        <ConfidenceThreshold>10.0</ConfidenceThreshold>
        <Valid>true</Valid>
        <Reviewed>true</Reviewed>
        <ReviewedBy>SYSTEM</ReviewedBy>
        <ValidatedBy>SYSTEM</ValidatedBy>
        <ErrorMessage/>
        <Value>HYPER SHIPPING'SDN BHD_First_Page</Value>  //Value to be replaced here
        <DocumentDisplayInfo/>
        <DocumentLevelFields/>
        <Pages>
            <Page>
                <Identifier>PG0</Identifier>
                <OldFileName>HYPER-KL FEB-0001-0001.tif</OldFileName>
                <NewFileName>BI2E7_0.tif</NewFileName>
                <SourceFileID>1</SourceFileID>
                <PageLevelFields>
                    <PageLevelField>
                        <Name>Search_Engine_Classification</Name>
                        <Value>Park Street '10 road</Value>     //Value to be replaced here
                        <Type/>
                        <Confidence>66.23</Confidence>
                        <LearnedFileName>HYPER KL-JUN-0001.tif</LearnedFileName>
                        <OcrConfidenceThreshold>0.0</OcrConfidenceThreshold>
                        <OcrConfidence>0.0</OcrConfidence>
                        <FieldOrderNumber>0</FieldOrderNumber>
                        <ForceReview>false</ForceReview>
                    </PageLevelField>
                </PageLevelFields>
            </Page>
        </Pages>
    </Document>
java xml dom xml 解析 jdom

评论

1赞 Stultuske 2/19/2021
到目前为止,你尝试了什么?
1赞 Stultuske 2/19/2021
这样做的原因:value =value.replace(“'”,“''”);帮不了你,是因为:a.是替换,应该是替换所有,可以有不止一个'和b:''和“不一样。你需要做一些类似 value.replaceAll(“'”, “\”“);但是,如果您尝试这样做,为什么不直接读取 String 中的整个 xml 文件呢?一个 replaceAll 也许能够解决您的整个问题。
0赞 NomadMaker 2/20/2021
String#replace() 将替换所有项目。它不使用正则表达式。

答:

0赞 Eduard A 2/19/2021 #1

您需要在单引号双引号上添加反斜杠

value =value.replace("\'","\"");
0赞 Sunny 2/19/2021 #2

只需将 removeQuote 方法替换为

private static void removeQuote(Document batchXml) throws JDOMException, Exception {
        Element root = batchXml.getRootElement();
        List<Element> docs = root.getChild("Documents").getChildren("Document");
        for (Element doc : docs) {
            String docType = doc.getChildText("Value");
             value =value.replaceAll("\'", "\"");
        }
    }
1赞 Md Kawser Habib 2/20/2021 #3

此代码可以将 all 替换为 XML 文件中的 all。'"

这里不添加任何描述,尝试一步一步地编码。这很容易理解。

(更新)

Part 1: Using JDOM

import java.util.ArrayList;
import java.util.List;

import org.w3c.dom.NodeList;
import org.jdom2.input.SAXBuilder;
import org.jdom2.transform.JDOMSource;
import org.w3c.dom.*;

import java.io.*;

public class XmlModificationJDom {

    public static void main(String[] args) {
        XmlModificationJDom xmlModificationJDom = new XmlModificationJDom();
        xmlModificationJDom.updateXmlAndSaveJDom();

    }

    public void updateXmlAndSaveJDom() {
        try {
            File inputFile = new File("document.xml");
            SAXBuilder saxBuilder = new SAXBuilder();
            org.jdom2.Document xmlDocument = saxBuilder.build(inputFile);
            org.jdom2.Element rootElement = xmlDocument.getRootElement();

            iterateAndUpdateElementsUsingJDom(rootElement);

            saveUpdatedXmlUsingJDomSource(xmlDocument);

        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }

    public void iterateAndUpdateElementsUsingJDom(org.jdom2.Element element) {

        if (element.getChildren().size() == 0) {
            // System.out.println(element.getName() + ","+ element.getText());
            if (element.getText().contains("'")) {
                element.setText(element.getText().replaceAll("\'", "\""));
            }
        } else {
            // System.out.println(element.getName());
            for (org.jdom2.Element childElement : element.getChildren()) {
                iterateAndUpdateElementsUsingJDom(childElement);
            }
        }
    }
}

Part 2: Using DOM

import javax.xml.parsers.*;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import java.util.ArrayList;
import java.util.List;

import java.io.*;

public class XmlModificationDom {

    public static void main(String[] args) {
        XmlModificationDom XmlModificationDom = new XmlModificationDom();
        XmlModificationDom.updateXmlAndSave();
    }
    
    public void updateXmlAndSave() {
        try {
            File inputFile = new File("document.xml");
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document document = dBuilder.parse(inputFile);
            document.getDocumentElement().normalize();

            Node parentNode = document.getFirstChild();
            iterateChildNodesAndUpate(parentNode);

            writeAndSaveXML(document);

        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }

    public void writeAndSaveXML(Document document) throws Exception {
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(document);
        StreamResult result = new StreamResult(new File("updated-document.xml"));
        transformer.transform(source, result);
    }

    public void iterateChildNodesAndUpate(Node parentNode) {

        NodeList nodeList = parentNode.getChildNodes();

        for (int index = 0; index < nodeList.getLength(); index++) {
            Node node = nodeList.item(index);
            if (node.getNodeType() == Node.ELEMENT_NODE) {
                Element element = (Element) node;
                //System.out.print(element.getNodeName());

                if (element.hasChildNodes() && element.getChildNodes().getLength() > 1) {
                    //System.out.println("Child > " + element.getNodeName());
                    iterateChildNodesAndUpate(element);
                } else {
                    //System.out.println(" - " + element.getTextContent());
                    if (element.getTextContent().contains("'")) {
                        String str = element.getTextContent().replaceAll("\'", "\"");
                        element.setTextContent(str);
                    }
                }
            }
        }
    }
}

输入文件 :document.xml

<Document>
        <Identifier>DOC1</Identifier>
        <Type>HYPER SHIPPING SDN BHD</Type>
        <Description>HYPER SHIPPING SDN BHD</Description>
        <Confidence>33.12</Confidence>
        <ConfidenceThreshold>10.0</ConfidenceThreshold>
        <Valid>true</Valid>
        <Reviewed>true</Reviewed>
        <ReviewedBy>SYSTEM</ReviewedBy>
        <ValidatedBy>SYSTEM</ValidatedBy>
        <ErrorMessage/>
        <Value>HYPER SHIPPING'SDN BHD_First_Page</Value>  //Value to be replaced here
        <DocumentDisplayInfo/>
        <DocumentLevelFields/>
        <Pages>
            <Page>
                <Identifier>PG0</Identifier>
                <OldFileName>HYPER-KL FEB-0001-0001.tif</OldFileName>
                <NewFileName>BI2E7_0.tif</NewFileName>
                <SourceFileID>1</SourceFileID>
                <PageLevelFields>
                    <PageLevelField>
                        <Name>Search_Engine_Classification</Name>
                        <Value>Park Street '10 road</Value>     //Value to be replaced here
                        <Type/>
                        <Confidence>66.23</Confidence>
                        <LearnedFileName>HYPER KL-JUN-0001.tif</LearnedFileName>
                        <OcrConfidenceThreshold>0.0</OcrConfidenceThreshold>
                        <OcrConfidence>0.0</OcrConfidence>
                        <FieldOrderNumber>0</FieldOrderNumber>
                        <ForceReview>false</ForceReview>
                    </PageLevelField>
                </PageLevelFields>
            </Page>
        </Pages>
</Document>

输出:updated-document.xml/updated-document-jdom.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Document>
        <Identifier>DOC1</Identifier>
        <Type>HYPER SHIPPING SDN BHD</Type>
        <Description>HYPER SHIPPING SDN BHD</Description>
        <Confidence>33.12</Confidence>
        <ConfidenceThreshold>10.0</ConfidenceThreshold>
        <Valid>true</Valid>
        <Reviewed>true</Reviewed>
        <ReviewedBy>SYSTEM</ReviewedBy>
        <ValidatedBy>SYSTEM</ValidatedBy>
        <ErrorMessage/>
        <Value>HYPER SHIPPING"SDN BHD_First_Page</Value><DocumentDisplayInfo/>
        <DocumentLevelFields/>
        <Pages>
            <Page>
                <Identifier>PG0</Identifier>
                <OldFileName>HYPER-KL FEB-0001-0001.tif</OldFileName>
                <NewFileName>BI2E7_0.tif</NewFileName>
                <SourceFileID>1</SourceFileID>
                <PageLevelFields>
                    <PageLevelField>
                        <Name>Search_Engine_Classification</Name>
                        <Value>Park Street "10 road</Value><Type/>
                        <Confidence>66.23</Confidence>
                        <LearnedFileName>HYPER KL-JUN-0001.tif</LearnedFileName>
                        <OcrConfidenceThreshold>0.0</OcrConfidenceThreshold>
                        <OcrConfidence>0.0</OcrConfidence>
                        <FieldOrderNumber>0</FieldOrderNumber>
                        <ForceReview>false</ForceReview>
                    </PageLevelField>
                </PageLevelFields>
            </Page>
        </Pages>
</Document>

更多详细信息代码,请访问此存储库

评论

0赞 Md Kawser Habib 2/20/2021
@Kashyap Savsani,如果这个答案有帮助并解决了你的问题,那么你可以接受并投票。请阅读此 stackoverflow.com/help/someone-answers
0赞 Kashyap Savsani 2/20/2021
感谢您的回答,这真的很有帮助。我知道您正在使用 DOM 解析器获取结果。我对使用 JDOM 解析器有特定的要求。我使用您的逻辑尝试了代码,并在后期编辑了示例代码。但是由于某种原因,仍然有些逻辑不起作用。你能看看并让我帮助改变吗?比如 if (node.getNodeType() == Node.ELEMENT_NODE) { Element element = (Element) node;
0赞 Md Kawser Habib 2/21/2021
对不起,我的误会。我更新了我的答案并为 JDOM 添加了代码。希望,你接受这个答案。
0赞 Kashyap Savsani 2/21/2021
对不起,如果您能提供帮助,我会收到此错误。在发布中附加更新的代码。我正在使用 JDOM1 而不是 JDOM2 错误:不兼容的类型:对象无法转换为 Element for(Element i: parentNode.getChildren()) { ^
0赞 Kashyap Savsani 2/21/2021
我设法处理了错误。以上是更新后的代码。多谢