Java 如何找到表达式并用特定文本替换它? 我有一个XML文件,在句子的中间有令牌。 例如:#他/她的#测试已完成

Java 如何找到表达式并用特定文本替换它? 我有一个XML文件,在句子的中间有令牌。 例如:#他/她的#测试已完成,java,xml,parsing,expression,tokenize,Java,Xml,Parsing,Expression,Tokenize,我想在xml文件中搜索任何#(text)#标记,并将其替换为其正确的代词,因此上面的标记将替换为他或她。如何搜索#(文本)表达式 我不知道如何使用标记器,如果这是我要使用的,也不知道如何为正则表达式设置权限 我正在完成一个别人开始的项目,这就是他们所拥有的,但他们无法让它工作。我只是想知道如何在xml文件中搜索标记 尝试一: File inputXML = new File("template.xml"); // creates new input file DocumentBu

我想在xml文件中搜索任何#(text)#标记,并将其替换为其正确的代词,因此上面的标记将替换为他或她。如何搜索#(文本)表达式

我不知道如何使用标记器,如果这是我要使用的,也不知道如何为正则表达式设置权限

我正在完成一个别人开始的项目,这就是他们所拥有的,但他们无法让它工作。我只是想知道如何在xml文件中搜索标记

尝试一:

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 ("#resp_he/she_lc#".equals(pronounList.item(count1).getTextContent())) {
                            pronounList.item(count1).setTextContent("he");
                        }

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

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

                        if ("#resp_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_caps#".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_lc#".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();
                }
            }
        }
File inputXML=新文件(“template.xml”);//创建新的输入文件
DocumentBuilderFactory解析器=DocumentBuilderFactory.newInstance();//docbuilder的新实例
DocumentBuilder dParser=parser.newDocumentBuilder();//叫它
Document doc=dParser.parse(inputXML);//解析文件
doc.getDocumentElement().normalize();
NodeList pList=doc.getElementsByTagName(“Verbiage”);//按标记名获取元素,并将其放入列表中以开始分析
int性别=1;//性别必须从响应文件中提取,它是硬编码的,用于测试目的
//System.out.println(“-------------------------------”)//新线
//循环遍历冗余标记列表
对于(int-temp=0;temp
在记事本中使用正则表达式++

^#.{0,}#$应该可以找到#

但我记不起是否需要逃逸。我不这么认为


此外,如果您需要特别查找他或她,您可以添加。^#。{0,}his.{0,}#$

您能帮助我们使用您尝试过的代码吗。
xmlString=xmlString.replace(“######,,“她”)?@Tauqir我还没有找到怎么做,所以我在这里哈哈。我不知道是使用标记器还是有其他方法来搜索表达式。@Felicia顺便说一句,将文件作为字符串读入的一种方法:
String content=new String(Files.readAllBytes(path.get(“template.xml”)对于大多数涉及转换XML的任务,您应该研究XSLT。要找到它,你可以用#\1His\2替换它#我不知道正则表达式是如何工作的。。。这是我问题的一部分哈哈。在记事本++中如何处理正则表达式?使用搜索/替换功能,我认为它在顶部的编辑下。使用^#(.{0,})His(.{0,})#$进行搜索。首先要确保它选择了正确的标签。并确保您已检查正则表达式代码必须能够自己执行此搜索,而不是手动执行此搜索。