2020년 4월 16일 목요일

java에서xml 파싱 해서 해쉬맵으로 담기

공통 모듈화 해본 부분
public static ArrayList<HashMap<String, String>> xmlForm (String strXml, String strNode){

HashMap<String, String> map = new HashMap<String,String>();
ArrayList <HashMap<String,String>> mapList = new ArrayList<HashMap<String,String>>();

String result = strXml;
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        DocumentBuilder builder;
        Document doc = null;
        try {
            // xml 파싱하기
            InputSource is = new InputSource(new StringReader(result));
            builder = factory.newDocumentBuilder();
            doc = builder.parse(is);
            XPathFactory xpathFactory = XPathFactory.newInstance();
            XPath xpath = xpathFactory.newXPath();
            XPathExpression expr = xpath.compile(strNode);
            NodeList nodeList = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
            for (int i = 0; i < nodeList.getLength(); i++) {
                NodeList child = nodeList.item(i).getChildNodes();
                map = new HashMap<String,String>();
                for (int j = 0; j < child.getLength(); j++) {
                    Node node = child.item(j);
                    logger.debug("현재 노드 이름 : "+node.getNodeName());
                    logger.debug("현재 노드 타입 : "+node.getNodeType());
                    logger.debug("현재 노드 값 : "+node.getTextContent());
                    logger.debug("현재 노드 네임스페이스 : "+node.getPrefix());
                    logger.debug("현재 노드의 다음 노드 : "+node.getNextSibling());
                    map.put(node.getNodeName().toString(), node.getTextContent().toString());
                }
                mapList.add(map);
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
return mapList;
}


호출 하는 부분
@RequestMapping(value = "/xmlForm", method = RequestMethod.POST)
public ArrayList<HashMap<String, String>> xmlForm (HttpServletRequest request) {
ArrayList <HashMap<String,String>> mapList = new ArrayList<HashMap<String,String>>();
            // xml 파싱하기 요청하기
        String result = "<S:Envelope xmlns:S='http://schemas.xmlsoap.org/soap/envelope/'>"+
"<S:Body>"+
"<ns2:executeResponse xmlns:ns2='http://pmsfc4002.pmsfc40.pms.lincoln.seanuts.co.jp/'>"+
"<return>"+
"<commonResponse>"+
"<isSuccess>true </isSuccess>"+
"</commonResponse>"+
"<adjustmentResult>"+
"<planGroupCode>0 </planGroupCode>"+
"<planGroupName>Webシングル</planGroupName>"+
"<adjustmentDate>20100201 </adjustmentDate>"+
"<priceRange1>14500 </priceRange1>"+
"<salesStatus>1 </salesStatus>"+
"<requestId>030 </requestId>"+
"<planList>"+
"<agtCode>170 </agtCode>"+
"<netAgtRmTypeCode>S01 </netAgtRmTypeCode>"+
"<planCode>S01*1 </planCode>"+
"<offereeCode>- </offereeCode>"+
"<planName>ビジネスシングルプラン(禁煙) </planName>"+
"<priceRange1>14500 </priceRange1>"+
"<netAgtRmTypeName>シングル(禁煙) </netAgtRmTypeName>"+
"<isPriceAdjustable>true </isPriceAdjustable>"+
"<salesStatus>1 </salesStatus>"+
"</planList>"+
"<planList>"+
"<agtCode>164 </agtCode>"+
"<netAgtRmTypeCode>00361 </netAgtRmTypeCode>"+
"<planCode>75710 </planCode>"+
"<offereeCode>- </offereeCode>"+
"<planName>シングルプラン</planName>"+
"<priceRange1>14500 </priceRange1>"+
"<netAgtRmTypeName>シングルルームA </netAgtRmTypeName>"+
"<isPriceAdjustable>true </isPriceAdjustable>"+
"<salesStatus>1 </salesStatus>"+
"</planList>"+
"<planList>"+
"<agtCode>166 </agtCode>"+
"<netAgtRmTypeCode>0027689 </netAgtRmTypeCode>"+
"<planCode>00078191 </planCode>"+
"<offereeCode>- </offereeCode>"+
"<planName>【ビジネスシングル】シンプルステイ(素泊まり) </planName>"+
"<priceRange1>14500 </priceRange1>"+
"<netAgtRmTypeName>シングル ルーム</netAgtRmTypeName>"+
"<isPriceAdjustable>true </isPriceAdjustable>"+
"<salesStatus>1 </salesStatus>"+
"</planList>"+
"</adjustmentResult>"+
"</return>"+
"</ns2:executeResponse>"+
"</S:Body>"+
"</S:Envelope>";
       
        System.out.println("-------------------------------------");
        System.out.println(StringUtil.xmlForm(result,"//planList"));
        System.out.println("-------------------------------------");
        mapList = StringUtil.xmlForm(result,"//planList");

return mapList;