2013-11-21 43 views
0

我有下面的plist。在xml文件中查找字符串並將其值分配給另一個字符串

<?xml version="1.5" encoding="UTF-8"?> 
<!DOCTYPE plist PUBLIC "-//PSect//ACD PLIST 1.5//" "http://pset.com/ACD/plist.dtd"> 
<plist version="1.5"> 
<dict> 
     <key>City</key> 
     <string>Melbourne</string> 
     <key>DetailedInfo</key> 
     <dict> 
       <key>Name</key> 
       <real>Sam</real> 
       <key>Income</key> 
       <real>4000</real> 
     </dict> 
     <key>Status</key> 
     <string>Single</string> 
     <key>PIN</key> 
     <string>123456789</string> 
</dict> 

我有解析這個plist到一個xml文件的代碼。我需要幫助的是在plist中找到關鍵城市。我已經看過一些帖子來搜索xml文件中的字符串,但沒有多少運氣。基本上我想要做的是,

1. Check if my xml file has Key City 
2. If it does, assign its value (Melbourne) to another String. 

有反正我可以做到這一點嗎?請建議。

+1

使用分析器... – Maroun

+0

請哪些API /庫您正在指定或顯示你到現在爲止的代碼。有一噸的方式做你想做的。爲了得到妥善解決,你需要顯示你到目前爲止已經嘗試過的東西 – Matthias

+0

@Maththias - 我只有解析xml文件的代碼,使用標準的文件I/O。 ic字符串,並將其值分配給另一個字符串。 – rickygrimes

回答

0

爲什麼不使用像jaxb或xtream這樣的object-xml綁定框架來執行此操作。那些框架會從你的xml中創建一個對象,然後很容易導航這個xml。例如,你可以做,如果(「城市」 .equals(getDict()。信息getKey()){然後執行此}

1

我不知道你在你的plist有DOCTYPE,但試試這個 試試這個

import java.io.File; 

import javax.xml.parsers.DocumentBuilder; 
import javax.xml.parsers.DocumentBuilderFactory; 

import org.w3c.dom.Document; 
import org.w3c.dom.NodeList; 

public class Demo { 

    public static void main(String[] args) throws Exception { 

     String keyVal = null; 
     DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
     DocumentBuilder db = dbf.newDocumentBuilder(); 
     Document document = db.parse(new File("input.xml")); 
     NodeList keyList = document.getElementsByTagName("key"); 

     if(keyList !=null && keyList.getLength() > 0) { 
      for(int i =0; i< keyList.getLength(); i++) { 
       keyVal = keyList.item(i).getTextContent(); 
       if ("City".equals(keyVal)) { 
        NodeList stringList = document.getElementsByTagName("string"); 
        if(stringList !=null && stringList.getLength() > 0) { 
         System.out.println(stringList.item(i).getTextContent()); 
        } 
       } 
      } 
     } 
    } 
} 
+0

如何在xml中找到它?我已經按照下面列出的步驟將文件內容讀入字符串 - http://www.mkyong.com/java/how-to-read-file-from-java-bufferedreader-example/因此,如果我必須檢查我的鑰匙串,那我該怎麼做? – rickygrimes

+0

欣賞你的答案。 +1爲您的努力,也可以是其中一個解決方案。 – rickygrimes

1
XPath path = XPathFactory.newInstance().newXPath(); 
    NodeList nl = (NodeList) path.evaluate("//dict[key/text()='City']", doc, XPathConstants.NODESET); 
    if (nl.getLength() == 1) { 
     Element dictElement = (Element) nl.item(0); 

     NodeList stringNodeList = dictElement.getElementsByTagName("string"); 
     for (int i = 0; i < stringNodeList.getLength(); i++) { 
      // replace string here 
      System.out.println("Replace: " + stringNodeList.item(i)); 
     } 
    } 
+0

如何在xml中找到它?我已經按照以下列出的步驟將文件內容讀入字符串 - mkyong.com/java/...因此,如果我必須檢查字符串是否爲我的密鑰,那麼我該怎麼做? – rickygrimes

+0

閱讀文檔:DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document document = db.parse(new File(「input.xml」)); – pasha701

相關問題