2016-10-04 49 views
-1

我在句子中間有一個帶有標記的xml文件。 例如:#his/her_caps#測試完成。如何查找表達式並將其替換爲某些文本?

我想在xml文件中搜索任何#(文本)#標記並將其替換爲它的合適代詞,因此上面的標記將替換爲他或她。如何搜索#(文本)#表達式?

我不明白如何使用tokenizer,如果這是我會用,不知道如何正確的正則表達式。

我正在完成別人開始的項目,這是他們的,但他們不能讓它工作。我只是想知道如何搜索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(); 
       } 
      } 
     } 
+0

你能幫我們解決你試過的代碼嗎? – Tauqir

+2

'xmlString = xmlString.replace(「## his/her_caps ##」,「她」);'? – Bohemian

+0

@Tauqir我還沒有找到如何做到這一點,爲什麼我在這裏哈哈。我不知道是否使用標記器,或者是否有其他方法來搜索表達式。 – Felicia

回答

0

使用正則表達式在記事本++

^#{0,}#$,應找出所有與#

的事情不記得了。 #需要被轉義(#)。我不這麼認爲。

此外,如果你需要找到他或她的具體可以添加。 ^#。{0,}他的。{0,}#$

+0

如果您使用^#(。{0,})his(。{0,})#$。要找到它,你可以用#\ 1His \ 2# –

+0

替換它。我不知道如何正則表達式工作...這是我的問題的一部分哈哈。我在記事本++中使用正則表達式做什麼? – Felicia

+0

使用搜索/替換功能,我認爲這是在頂部編輯。 –