'DocumentBuilder'에 해당되는 글 1건

  1. 2007/06/14 XML 예제, 샘플 코드
2007/06/14 10:31

XML 예제, 샘플 코드

try {
   DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
   DocumentBuilder db = dbf.newDocumentBuilder();
   Document doc = null;
   doc = db.parse(new File("test.xml"));

   doc.getDocumentElement().normalize();
   System.out.println("Root element " + doc.getDocumentElement().getNodeName());

   NodeList nodeLst = doc.getElementsByTagName("que");
   int nodeCount = nodeLst.getLength();

   String[] saQueName = new String[nodeCount];
   String[] saInDto = new String[nodeCount];
   String[] saOutDto = new String[nodeCount];

   for (int inx = 0; inx < nodeCount; inx++) {

    Node node = nodeLst.item(inx);

    if (node.getNodeType() == Node.ELEMENT_NODE) {

     Element ele = (Element) node;

     NodeList queName = ele.getElementsByTagName("que-name");
     Element queNameElmnt = (Element) queName.item(0);
     //System.out.println("que-name : " + queNameElmnt.getChildNodes().item(0).getNodeValue());
     saQueName[inx] = queNameElmnt.getChildNodes().item(0).getNodeValue();


     NodeList input = ele.getElementsByTagName("input");
     Element inputElmnt = (Element) input.item(0);
     //System.out.println("input : " + inputElmnt.getChildNodes().item(0).getNodeValue());
     saInDto[inx] = inputElmnt.getChildNodes().item(0).getNodeValue();


     NodeList output = ele.getElementsByTagName("output");
     Element outputElmnt = (Element) output.item(0);
     //System.out.println("outpu : " + outputElmnt.getChildNodes().item(0).getNodeValue());
     saOutDto[inx] = outputElmnt.getChildNodes().item(0).getNodeValue();
    }

   }

   for(int i = 0 ; i < nodeCount ; i++) {
    System.out.println("Que-Name : " + saQueName[i]);
    System.out.println("input-Name : " + saInDto[i]);
    System.out.println("output-Name : " + saOutDto[i]);
   }
  } catch (Exception e) {
   
  }

크리에이티브 커먼즈 라이선스
Creative Commons License
Trackback 0 Comment 0