繁体   English   中英

如何替换xml文件中的一部分字符串?

[英]How to replace a part of a string in an xml file?

我有一个类似这样的 xml 文件:

<Verbiage>
        The whiskers plots are based on the responses of incarcerated 
        <Choice>
            <Juvenile> juveniles who have committed sexual offenses. </Juvenile>
            <Adult> adult sexual offenders. </Adult>
        </Choice> 
        If the respondent is a 
        <Choice>
            <Adult>convicted sexual offender, </Adult>
            <Juvenile>juvenile who has sexually offended, </Juvenile>
        </Choice> 
        #his/her_lc# percentile score, which defines #his/her_lc# position 
        relative to other such offenders, should be taken into account as well as #his/her_lc# T score. Percentile 
        scores in the top decile (> 90 %ile) of such offenders suggest that the respondent 
        may be defensive and #his/her_lc# report should be interpreted with this in mind.
    </Verbiage>

我试图找到一种解析 xml 文件的方法(我一直在使用 DOM),搜索 #his/her_lc# 并将其替换为“她”。 我试过使用 FileReader、BufferedReader、string.replaceAll、FileWriter,但这些都不起作用。

有没有办法使用 XPath 来做到这一点?

最终我想在这个 xml 文件中搜索这个字符串并将其替换为另一个字符串。

我是否必须在字符串周围添加一个标签我希望它以这种方式解析它?

我试过的代码:

protected void parse() throws ElementNotValidException {
    try {
        //Parse xml File
        File inputXML = new File("template.xml");
        DocumentBuilderFactory parser = DocumentBuilderFactory.newInstance(); // new instance of doc builder
        DocumentBuilder dParser = parser.newDocumentBuilder(); // calls it
        Document doc = dParser.parse(inputXML); // parses file

        FileReader reader = new FileReader(inputXML);
        String search = "#his/her_lc#";
        String newString;

        BufferedReader br = new BufferedReader(reader);
        while ((newString = br.readLine()) != null){
            newString.replaceAll(search, "her");
        }

        FileWriter writer = new FileWriter(inputXML);
        writer.write(newString);
        writer.close();

    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (SAXException e) {
        e.printStackTrace();
    }

我被给予修复的代码:

try {
        File inputXML = new File("template.xml"); // creates new input file
        DocumentBuilderFactory parser = DocumentBuilderFactory.newInstance(); // new instance of doc builder
        DocumentBuilder dParser = parser.newDocumentBuilder(); // calls it
        Document doc = dParser.parse(inputXML); // parses file
        doc.getDocumentElement().normalize();

        NodeList pList = doc.getElementsByTagName("Verbiage"); // gets element by tag name and places into list to begin parsing

        int gender = 1; // gender has to be taken from the response file, it is hard coded for testing purposes
        System.out.println("----------------------------"); // new line

        // loops through the list of Verbiage tags
        for (int temp = 0; temp < pList.getLength(); temp++) {
            Node pNode = pList.item(0); // sets node to temp

            if (pNode.getNodeType() == Node.ELEMENT_NODE) { // if the node type = the element node
                Element eElement = (Element) pNode;
                NodeList pronounList = doc.getElementsByTagName("pronoun"); // gets a list of pronoun element tags

                if (gender == 0) { // if the gender is male

                    int count1 = 0;
                    while (count1 < pronounList.getLength()) {

                        if ("#he/she_lc#".equals(pronounList.item(count1).getTextContent())) {
                            pronounList.item(count1).setTextContent("he");
                        }

                        if ("#he/she_caps#".equals(pronounList.item(count1).getTextContent())) {
                            pronounList.item(count1).setTextContent("He");
                        }

                        if ("#his/her_lc#".equals(pronounList.item(count1).getTextContent())) {
                            pronounList.item(count1).setTextContent("his");
                        }
                        if ("#his/her_caps#".equals(pronounList.item(count1).getTextContent())) {
                            pronounList.item(count1).setTextContent("His");
                        }

                        if ("#him/her_lc#".equals(pronounList.item(count1).getTextContent())) {
                            pronounList.item(count1).setTextContent("him");
                        }
                        count1++;
                    }
                    pNode.getNextSibling();

                } else if (gender == 1) { // female
                    int count = 0;
                    while (count < pronounList.getLength()) {

                        if ("#he/she_lc#".equals(pronounList.item(count).getTextContent())) {
                            pronounList.item(count).setTextContent("she");
                        }

                        if ("#he/she_caps3".equals(pronounList.item(count).getTextContent())) {
                            pronounList.item(count).setTextContent("She");
                        }

                        if ("#his/her_lc#".equals(pronounList.item(count).getTextContent())) {
                            pronounList.item(count).setTextContent("her");
                        }
                        if ("#his/her_caps#".equals(pronounList.item(count).getTextContent())) {
                            pronounList.item(count).setTextContent("Her");
                        }

                        if ("#him/her_lc#".equals(pronounList.item(count).getTextContent())) {
                            pronounList.item(count).setTextContent("her");
                        }
                        count++;
                    }
                    pNode.getNextSibling();
                }
            }
        }
        // write the content to file
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(doc);

        System.out.println("-----------Modified File-----------");
        StreamResult consoleResult = new StreamResult(System.out);
        transformer.transform(source, new StreamResult(new FileOutputStream("template.xml"))); // writes changes to file
    } catch (Exception e) {
        e.printStackTrace();
    }

}

如果我能弄清楚如何将标记 Pronoun 与此代码所在的 pronounParser 相关联,我认为这段代码会起作用。

我使用了这个例子和你的 template.xml,我认为它有效。

public static void main(String[] args) {

        File inputXML = new File("template.xml");
        BufferedReader br = null;
        String newString = "";
        StringBuilder strTotale = new StringBuilder();
        try {

        FileReader reader = new FileReader(inputXML);
        String search = "#his/her_lc#";


        br = new BufferedReader(reader);
        while ((newString = br.readLine()) != null){
            newString = newString.replaceAll(search, "her");
            strTotale.append(newString);
        }

        } catch ( IOException  e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } // calls it
        finally
        {
            try {
                br.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }


        System.out.println(strTotale.toString());


    }

首先,您必须重新分配 replaceAll 的结果:

newString = newString.replaceAll(search, "her");

其次,我使用 StringBuffer 来收集所有行。

我希望这会有所帮助。

由于字符串是不可变的,您不能修改它们,请使用 StringBuilder/StringBuffer 而不是 String。

FileReader reader = new FileReader(inputXML);
    String search = "#his/her_lc#";
    String newString;
    StringBuffer str;

    BufferedReader br = new BufferedReader(reader);
    while ((newString = br.readLine()) != null){
        str.append(newString.replaceAll(search, "her"));
    }

    FileWriter writer = new FileWriter(inputXML);
    writer.write(str);
    writer.close();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM