2016-10-04 10 views
-1

私は、文の途中にトークンを含むxmlファイルを持っています。 例:#his/her_caps#テストが完了しました。式を見つけて特定のテキストに置き換えるにはどうすればよいですか?

#(テキスト)#トークンのxmlファイルを検索し、適切な代名詞に置き換えたいので、上記のタグをHisまたはHerに置き換えます。 #(テキスト)#式を検索するにはどうすればよいですか?

私はトークンの使用方法を理解していませんし、それに正規表現をする方法もわからないのです。

私は他の誰かが始めたプロジェクトを仕上げていますが、これは彼らが持っていたものですが、動作させることができませんでした。 XMLファイルでタグを検索する方法を知りたいだけです。

試み1:

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 ## "、" HER ");'? – Bohemian

+0

@タクワール私はそれを行う方法を見つけていないので、なぜ私はここにいるのですか。私はトークナイザを使用するかどうか、または式を検索する別の方法があるかどうかはわかりません。 – Felicia

答えて

0

使用正規表現メモ帳で++

^#{0、}#$は場合#

間のすべてのものは覚えてすることはできません見つける必要があります。しかし、#をエスケープする必要があります(#)。私はそうは思わない。

また、彼または彼女を具体的に見つける必要がある場合は、追加することもできます。 ^#。{0、} his。{0、}#$

+0

^(。{0、})の彼(。{0、})#$を使用すると、それを見つけるには#\ 1His \ 2# –

+0

と置き換えることができます私はどのように正規表現が動作するのか分かりません...これは私の問題hahaの一部です。私はnotepad ++の正規表現で何をしますか? – Felicia

+0

検索/置換機能を使用してください。編集中です。 –

関連する問題