2014-06-24 21 views
0

我有一個XML文件如何從Java類在ADF創建數據的控制和查看對象

<employees> 
    <employee id="111"> 
    <firstName>Rakesh</firstName> 
    <lastName>Mishra</lastName> 
    <location>Bangalore</location> 
    </employee> 
    <employee id="112"> 
    <firstName>John</firstName> 
    <lastName>Davis</lastName> 
    <location>Chennai</location> 
    </employee> 
    <employee id="113"> 
    <firstName>Rajesh</firstName> 
    <lastName>Sharma</lastName> 
    <location>Pune</location> 
    </employee> 
</employees> 

我使用解編入Employee類

public class DOMParserDemo { 

    public static void main(String[] args) throws Exception { 
    //Get the DOM Builder Factory 
    DocumentBuilderFactory factory = 
     DocumentBuilderFactory.newInstance(); 

    //Get the DOM Builder 
    DocumentBuilder builder = factory.newDocumentBuilder(); 

    //Load and Parse the XML document 
    //document contains the complete XML as a Tree. 
    Document document = 
     builder.parse(
     ClassLoader.getSystemResourceAsStream("xml/employee.xml")); 

    List<Employee> empList = new ArrayList<>(); 

    //Iterating through the nodes and extracting the data. 
    NodeList nodeList = document.getDocumentElement().getChildNodes(); 

    for (int i = 0; i < nodeList.getLength(); i++) { 

     //We have encountered an <employee> tag. 
     Node node = nodeList.item(i); 
     if (node instanceof Element) { 
     Employee emp = new Employee(); 
     emp.id = node.getAttributes(). 
      getNamedItem("id").getNodeValue(); 

     NodeList childNodes = node.getChildNodes(); 
     for (int j = 0; j < childNodes.getLength(); j++) { 
      Node cNode = childNodes.item(j); 

      //Identifying the child tag of employee encountered. 
      if (cNode instanceof Element) { 
      String content = cNode.getLastChild(). 
       getTextContent().trim(); 
      switch (cNode.getNodeName()) { 
       case "firstName": 
       emp.firstName = content; 
       break; 
       case "lastName": 
       emp.lastName = content; 
       break; 
       case "location": 
       emp.location = content; 
       break; 
      } 
      } 
     } 
     empList.add(emp); 
     } 

    } 

    //Printing the Employee list populated. 
    for (Employee emp : empList) { 
     System.out.println(emp); 
    } 

    } 
} 

我想在ADF中創建一個用戶界面,它將填充輸出數據的字段。

有人請指導我如何實現它?

+0

很好的例子http://www.mkyong.com/java/jaxb-hello-world-example/ – Shadi

回答