2016-11-03 19 views
0

希望你們都好。 我有一個需求,我需要從XML文件中可用的佔位符中選擇變量名稱。 我有一個XML文件具有所有佔位符,並且這些佔位符以$符號開頭。 我的任務是獲得該佔位符,並從它我需要獲取變量名稱。Java程序從XML文件中提供的佔位符中獲取變量名

例如,如果XML文件具有像$ Variable1那樣的佔位符,那麼它將從該佔位符獲得Variable1。

以下是我使用的代碼:以下是getConstant方法

public static String replaceConfigParam(String xmlFile, Object structure) { 
    for (String key : getConstants(xmlFile)) { 
     String actualKey = (new StringBuilder().append("$*").append(key).append("$")).toString(); 

     try { 

      String value = BeanUtils.getProperty(structure, key); 
      if (value != null) { 
       xmlFile = xmlFile.replace(actualKey, value); 
      } 

     } catch (IllegalAccessException e) { 
      logger.error("failed to get the property from object " + e.getMessage()); 
     } catch (InvocationTargetException e) { 
      logger.error("failed to get the property from object" + e.getMessage()); 
     } catch (NoSuchMethodException e) { 
      logger.error("failed to get the property from object " + e.getMessage()); 
     } catch (Exception e) { 
      logger.error("failed to get value from the property " + e.getMessage()); 
     } 
    } 
    return xmlFile; 

private static List<String> getConstants(String domainConfig) { 
    String[] arr = domainConfig.split("\\$"); 
    List<String> paramsExtracted = new ArrayList<String>(); 
    for (String key : arr) { 
     paramsExtracted.add(key.replace("$", "")); 
       } 
    return paramsExtracted; 
} 

以下是有它的美元XML文件,我需要提取變量從相同的文件:

<tunnel> 
     <units> 
      <entry name="tunnel.1"> 
      <ip> 
       <entry name="$ABC"/> 
      </ip> 
      <interface-management-profile>mgt</interface-management-profile> 
      </entry> 
     </units> 
     </tunnel> 

在此先感謝。

+0

沒有問題嗎? – pandaadb

+0

以上我問過這個問題? – Prax

+0

我的意思是說,沒有句子以「我該怎麼做」開頭。「我該怎麼做......」這看起來像你已經回答了你自己的問題?什麼不行?你有例外嗎?你有性能問題嗎? XML的外觀是什麼?我從字面上不知道它是什麼,你需要幫助:) http://stackoverflow.com/help/how-to-ask – pandaadb

回答

2

仍然不確定您實際要求。如果你有一個特定的問題,你應該描述這個問題。說清楚你想知道什麼,你的問題在哪裏以及你需要什麼幫助。 https://stackoverflow.com/help/how-to-ask

我假設你的問題是:

「如何提取條目名稱屬性由開始時的$ - 標號標識的所有變量」。

你可以用正則表達式做到這一點,但因爲你是XML,您可以使用XPath +的XPath解析器。在這裏看到:

import java.io.IOException; 
import java.io.StringReader; 

import javax.xml.parsers.DocumentBuilder; 
import javax.xml.parsers.DocumentBuilderFactory; 
import javax.xml.parsers.ParserConfigurationException; 
import javax.xml.xpath.XPathConstants; 
import javax.xml.xpath.XPathExpression; 
import javax.xml.xpath.XPathExpressionException; 
import javax.xml.xpath.XPathFactory; 

import org.w3c.dom.Document; 
import org.w3c.dom.Node; 
import org.w3c.dom.NodeList; 
import org.xml.sax.InputSource; 
import org.xml.sax.SAXException; 

public class ExtractVar { 

    static String xml = "<tunnel>" + 
    "<units>" + 
     "<entry name=\"tunnel.1\">"+ 
     "<ip>"+ 
     " <entry name=\"$ABC\"/>"+ 
     " </ip>"+ 
    " <interface-management-profile>mgt</interface-management-profile>"+ 
    " </entry>"+ 
    " </units>"+ 
    "</tunnel>"; 


    public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException, XPathExpressionException { 
     DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 
     Document document = db.parse(new InputSource(new StringReader(xml))); 

     XPathFactory xpathFactory = XPathFactory.newInstance(); 
     XPathExpression xpath = xpathFactory.newXPath().compile("//entry"); 

     NodeList entryNodes = (NodeList) xpath.evaluate(document, XPathConstants.NODESET); 
     for(int i =0; i<entryNodes.getLength(); i++) { 
      Node n = entryNodes.item(i); 
      String nodeValue = n.getAttributes().getNamedItem("name").getNodeValue(); 
      if(nodeValue.startsWith("$")) { 
       System.out.println(nodeValue.substring(1, nodeValue.length())); 
      } 
     } 
    } 
} 

下面的代碼所做的:

  1. 解析文檔(你的XML)爲DOM模型。
  2. 爲您希望分析的屬性創建一個Xpath表達式。在這種情況下,您需要所有名爲「entry」的節點,而不管它們在文檔中的位置。這是通過在Xpath //entry開始時執行//來實現的。
  3. 檢索入口節點的name屬性並檢查它是否有一個美元符號開始。 4.打印屬性值,如果它確實以$符號開頭。

的代碼,然後打印:

ABC 

我希望這是你所期待的。

或者這可以通過使用純的正則表達式捕捉每一個由引號包圍,並以美元符號開頭的字符串來實現,具體如下:

Pattern pattern = Pattern.compile("\"\\$(.*)\""); 
    Matcher matcher = pattern.matcher(xml); 
    while(matcher.find()) { 
     System.out.println(matcher.group(1)); 
    } 

阿圖爾

+0

我們如何在以下內容中添加< and >: Pattern pattern = Pattern.compile(「\」\\ $(。*)\「」); – Prax

+0

你想要捕捉什麼? – pandaadb