2013-09-26 3 views
0

위치 노드 값을 파싱/가져 오는 방법 : 인도 뉴 델리, 루핑을 사용하는 인도 및 이와 유사하게 SVM, HHHHH 등과 같은 다중 교육 노드 값을이 XML 파일에서 가져 오는 방법. 는 XML :XML 응답에서 노드 데이터를 구문 분석/가져 오는 방법은 무엇입니까?

<?xml version="1.0" encoding="UTF-8"?> 
<facebookfriends> 
<data> 
<id> 
[Removed] 
    </id> 
    <location> 
     <id> 
      [Removed] 
     </id> 
     <name> 
      New Delhi, India 
     </name> 
    </location> 
    <name> 
     XYZZZZZZZZZZ 
    </name> 
    <education> 
     <school> 
      <id> 
       [Removed] 
      </id> 
      <name> 
       SVM 
      </name> 
     </school> 
     <year> 
      <id> 
       [Removed] 
      </id> 
      <name> 
       2001 
      </name> 
     </year> 
     <type> 
      High School 
     </type> 
    </education> 
    <education> 
     <concentration> 
      <id> 
       [Removed] 
      </id> 
      <name> 
       Computer Science 
      </name> 
     </concentration> 
     <school> 
      <id> 
       [Removed] 
      </id> 
      <name> 
       HHHHHHHHHHHHH 
      </name> 
     </school> 
     <type> 
      Graduate School 
     </type> 
</education> 
</data> 

</facebookfriends> 

답변

0

아래 코드는 xml에서 위치/이름 노드 값을 파싱하여 얻는 데 도움이됩니다. 마찬가지로 xpath를 변경하여 다른 노드 값을 얻을 수 있습니다.

 File xmlFile = new File("C:/face.xml"); 
     DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); 
     DocumentBuilder builder; 
     try{ 
      builder = builderFactory.newDocumentBuilder(); 
      Document document = builder.parse(xmlFile); 
      XPath xPath = XPathFactory.newInstance().newXPath(); 
      String exp = "/facebookfriends/data/location/name"; 
      NodeList nodeList = (NodeList) xPath.compile(exp).evaluate(document, XPathConstants.NODESET); 
      for (int i = 0; i < nodeList.getLength(); i++) { 
       System.out.println(nodeList.item(i).getFirstChild().getNodeValue()); 
      } 
     }catch (ParserConfigurationException e) { 
      e.printStackTrace(); 
     } catch (XPathExpressionException e) { 
      e.printStackTrace(); 
     } 
관련 문제