java 解析xml字符串 拿到其中的数据

需要导入dom4j包
下载地址:https://mvnrepository.com/search?q=dom4j
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
有jar包 也有maven的方式

String xml = "<Request>" +
                "<Header>" +
                "<SourceSystem>测试SourceSystem</SourceSystem> " +
                "<MessageID>测试MessageID</MessageID>" +
                "</Header>" +
                "<Body>" +
                "<CT_MedicineMaterialList>" +
                "<CT_MedicineMaterial>" +
                "<CTMM_Code>测试CTMM_Code</CTMM_Code>" +
                "<CTMM_Desc>测试CTMM_Desc</CTMM_Desc>" +
                "<CTMM_GenericName>测试CTMM_GenericName</CTMM_GenericName>" +
                "<CTMM_AnotherName>测试CTMM_AnotherName</CTMM_AnotherName>" +
                "<CTMM_CodesystemCode>测试CTMM_CodesystemCode</CTMM_CodesystemCode>" +
                "<CTMM_Specification>测试CTMM_Specification</CTMM_Specification>" +
                "<CTMM_TradeName>测试CTMM_TradeName</CTMM_TradeName>" +
                "<CTMM_DosageFormDesc>测试CTMM_DosageFormDesc</CTMM_DosageFormDesc>" +
                "<CTMM_UnitPrice>测试CTMM_UnitPrice</CTMM_UnitPrice>" +
                "<CTMM_ManufacturerName>测试CTMM_ManufacturerName</CTMM_ManufacturerName>" +
                "<CTMM_ValuationUnit>测试CTMM_ValuationUnit</CTMM_ValuationUnit>" +
                "<CTMM_RetailPrice>测试CTMM_RetailPrice</CTMM_RetailPrice>" +
                "<CTMM_PackingUnit>测试CTMM_PackingUnit</CTMM_PackingUnit>" +
                "<CTMM_PackingCoefficient>测试CTMM_PackingCoefficient</CTMM_PackingCoefficient>" +
                "<CTMM_OrderCategory>测试CTMM_OrderCategory</CTMM_OrderCategory>" +
                "<CTMM_OrderChildCategory>测试CTMM_OrderChildCategory</CTMM_OrderChildCategory>" +
                "<CTMM_PharmacologyCategory>测试CTMM_PharmacologyCategory</CTMM_PharmacologyCategory>" +
                "<CTMM_PharmacologyChildCategory>测试CTMM_PharmacologyChildCategory</CTMM_PharmacologyChildCategory>" +
                "<CTMM_PharmacologySmallCategory>测试CTMM_PharmacologySmallCategory</CTMM_PharmacologySmallCategory>" +
                "<CTMM_Notice>测试CTMM_Notice</CTMM_Notice>" +
                "<CTMM_Indications>测试CTMM_Indications</CTMM_Indications>" +
                "<CTMM_Contraindication>测试CTMM_Contraindication</CTMM_Contraindication>" +
                "<CTMM_UntowardEffect>测试CTMM_UntowardEffect</CTMM_UntowardEffect>" +
                "<CTMM_Interaction>测试CTMM_Interaction</CTMM_Interaction>" +
                "<CTMM_Reminder>测试CTMM_Reminder</CTMM_Reminder>" +
                "<CTMM_EssentialMedicine>测试CTMM_EssentialMedicine</CTMM_EssentialMedicine>" +
                "<CTMM_ValuableSign>测试CTMM_ValuableSign</CTMM_ValuableSign>" +
                "<CTMM_EffectiveDate>测试CTMM_EffectiveDate</CTMM_EffectiveDate>" +
                "<CTMM_HealthInsuranceSign>测试CTMM_HealthInsuranceSign</CTMM_HealthInsuranceSign>" +
                "<CTMM_HealthInsuranceCoding>测试CTMM_HealthInsuranceCoding</CTMM_HealthInsuranceCoding>" +
                "<CTMM_SkinTestSign>测试CTMM_SkinTestSign</CTMM_SkinTestSign>" +
                "<CTMM_HealthInsuranceCategory>测试CTMM_HealthInsuranceCategory</CTMM_HealthInsuranceCategory>" +
                "<CTMM_Direction>测试CTMM_Direction</CTMM_Direction>" +
                "<CTMM_LimitedCharacteristic>测试CTMM_LimitedCharacteristic</CTMM_LimitedCharacteristic>" +
                "<CTMM_AgeLimit>测试CTMM_AgeLimit</CTMM_AgeLimit>" +
                "<CTMM_CriticalCareMedicine>测试CTMM_CriticalCareMedicine</CTMM_CriticalCareMedicine>" +
                "<CTMM_AntibacterialSign>测试CTMM_AntibacterialSign</CTMM_AntibacterialSign>" +
                "<CTMM_StatusCode>测试CTMM_StatusCode</CTMM_StatusCode>" +
                "<CTMM_StartDate>测试CTMM_StartDate</CTMM_StartDate>" +
                "<CTMM_EndDate>测试CTMM_EndDate</CTMM_EndDate>" +
                "<CTMM_DosUom>测试CTMM_DosUom</CTMM_DosUom>" +
                "<CTMM_Dosage>测试CTMM_Dosage</CTMM_Dosage>" +
                "<CTMM_MinPkgNum>测试CTMM_MinPkgNum</CTMM_MinPkgNum>" +
                "</CT_MedicineMaterial>" +
                "</CT_MedicineMaterialList>" +
                "</Body>" +
                "</Request>";
        try {
            //将xml转编码 如果不转有可能出现乱码
            xml = new String(xml.getBytes("GBK"), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        Document document = null;
        try {
            document = DocumentHelper.parseText(xml);
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        // 转编码
        document.setXMLEncoding("utf-8");
        //获取到最大的节点   Request
        Element root = document.getRootElement();
        //获取    Header  的节点信息
        Element header = root.element("Header");

        String SourceSystem = header.elementText("SourceSystem");                       
        String MessageID = header.elementText("MessageID");                         

        //获取    Body--->CT_MedicineMaterialList--->CT_MedicineMaterial  的节点信息
        Element body = root.element("Body").element("CT_MedicineMaterialList").element("CT_MedicineMaterial");

        String CTMM_Code = body.elementText("CTMM_Code");                          
        String CTMM_Desc = body.elementText("CTMM_Desc");                        
        String CTMM_GenericName = body.elementText("CTMM_GenericName");                  

        String CTMM_AnotherName = body.elementText("CTMM_AnotherName");                  
        String CTMM_CodesystemCode = body.elementText("CTMM_CodesystemCode");              
        String CTMM_Specification = body.elementText("CTMM_Specification");                 
        String CTMM_TradeName = body.elementText("CTMM_TradeName");                    
        String CTMM_DosageFormDesc = body.elementText("CTMM_DosageFormDesc");                
        String CTMM_UnitPrice = body.elementText("CTMM_UnitPrice");                    
        String CTMM_ManufacturerName = body.elementText("CTMM_ManufacturerName");             
        String CTMM_ValuationUnit = body.elementText("CTMM_ValuationUnit");                
        String CTMM_RetailPrice =  body.elementText("CTMM_RetailPrice");             
        String CTMM_PackingUnit   =  body.elementText("CTMM_PackingUnit");             

        String CTMM_PackingCoefficient = body.elementText("CTMM_PackingCoefficient");                
        String CTMM_OrderCategory = body.elementText("CTMM_OrderCategory");                     
        String CTMM_OrderChildCategory = body.elementText("CTMM_OrderChildCategory");                
        String CTMM_PharmacologyCategory = body.elementText("CTMM_PharmacologyCategory");              
        String CTMM_PharmacologyChildCategory = body.elementText("CTMM_PharmacologyChildCategory");         
        String CTMM_PharmacologySmallCategory = body.elementText("CTMM_PharmacologySmallCategory");         
        String CTMM_Notice = body.elementText("CTMM_Notice");                        
        String CTMM_Indications = body.elementText("CTMM_Indications");               
        String CTMM_Contraindication = body.elementText("CTMM_Contraindication");                  
        String CTMM_UntowardEffect = body.elementText("CTMM_UntowardEffect");                    

        String CTMM_Interaction = body.elementText("CTMM_Interaction");                 
        String CTMM_Reminder = body.elementText("CTMM_Reminder");                         
        String CTMM_EssentialMedicine = body.elementText("CTMM_EssentialMedicine");                 
        String CTMM_ValuableSign = body.elementText("CTMM_ValuableSign");                  
        String CTMM_EffectiveDate = body.elementText("CTMM_EffectiveDate");                     
        String CTMM_HealthInsuranceSign = body.elementText("CTMM_HealthInsuranceSign");               
        String CTMM_HealthInsuranceCoding = body.elementText("CTMM_HealthInsuranceCoding");             
        String CTMM_SkinTestSign = body.elementText("CTMM_SkinTestSign");                     
        String CTMM_HealthInsuranceCategory = body.elementText("CTMM_HealthInsuranceCategory");           
        String CTMM_Direction = body.elementText("CTMM_Direction");                       

        String CTMM_LimitedCharacteristic = body.elementText("CTMM_LimitedCharacteristic");             
        String CTMM_AgeLimit = body.elementText("CTMM_AgeLimit");                        
        String CTMM_CriticalCareMedicine = body.elementText("CTMM_CriticalCareMedicine");          
        String CTMM_AntibacterialSign = body.elementText("CTMM_AntibacterialSign");             
        String CTMM_StatusCode= body.elementText("CTMM_StatusCode");                 
        String CTMM_StartDate= body.elementText("CTMM_StartDate");                      
        String CTMM_EndDate= body.elementText("CTMM_EndDate");                       
        String CTMM_DosUom = body.elementText("CTMM_DosUom");                      
        String CTMM_Dosage = body.elementText("CTMM_Dosage");                       
        String CTMM_MinPkgNum = body.elementText("CTMM_MinPkgNum");                

        String data =
                "SourceSystem = '"+SourceSystem+"' , " +
                "MessageID = '"+MessageID+"' , " +
                "CTMM_Desc = '"+CTMM_Desc+"' , " +
                "CTMM_GenericName = '"+CTMM_GenericName+"' , " +
                "CTMM_AnotherName = '"+CTMM_AnotherName+"' , " +
                "CTMM_CodesystemCode = '"+CTMM_CodesystemCode+"' , " +
                "CTMM_Specification = '"+CTMM_Specification+"' , " +
                "CTMM_TradeName = '"+CTMM_TradeName+"' , " +
                "CTMM_DosageFormDesc = '"+CTMM_DosageFormDesc+"' , " +
                "CTMM_UnitPrice = '"+CTMM_UnitPrice+"' , " +
                "CTMM_ManufacturerName = '"+CTMM_ManufacturerName+"' , " +
                "CTMM_ValuationUnit = '"+CTMM_ValuationUnit+"' , " +
                "CTMM_RetailPrice = '"+CTMM_RetailPrice+"' , " +
                "CTMM_PackingUnit = '"+CTMM_PackingUnit+"' , " +
                "CTMM_PackingCoefficient = '"+CTMM_PackingCoefficient+"' , " +
                "CTMM_OrderCategory = '"+CTMM_OrderCategory+"' , " +
                "CTMM_OrderChildCategory = '"+CTMM_OrderChildCategory+"' , " +
                "CTMM_PharmacologyCategory = '"+CTMM_PharmacologyCategory+"' , " +
                "CTMM_PharmacologyChildCategory = '"+CTMM_PharmacologyChildCategory+"' , " +
                "CTMM_PharmacologySmallCategory = '"+CTMM_PharmacologySmallCategory+"' , " +
                "CTMM_Notice = '"+CTMM_Notice+"' , " +
                "CTMM_Indications = '"+CTMM_Indications+"' , " +
                "CTMM_Contraindication = '"+CTMM_Contraindication+"' , " +
                "CTMM_UntowardEffect = '"+CTMM_UntowardEffect+"' , " +
                "CTMM_Interaction = '"+CTMM_Interaction+"' , " +
                "CTMM_Reminder = '"+CTMM_Reminder+"' , " +
                "CTMM_EssentialMedicine = '"+CTMM_EssentialMedicine+"' , " +
                "CTMM_ValuableSign = '"+CTMM_ValuableSign+"' , " +
                "CTMM_EffectiveDate = '"+CTMM_EffectiveDate+"' , " +
                "CTMM_HealthInsuranceSign = '"+CTMM_HealthInsuranceSign+"' , " +
                "CTMM_HealthInsuranceCoding = '"+CTMM_HealthInsuranceCoding+"' , " +
                "CTMM_SkinTestSign = '"+CTMM_SkinTestSign+"' , " +
                "CTMM_HealthInsuranceCategory = '"+CTMM_HealthInsuranceCategory+"' , " +
                "CTMM_Direction = '"+CTMM_Direction+"' , " +
                "CTMM_LimitedCharacteristic = '"+CTMM_LimitedCharacteristic+"' , " +
                "CTMM_AgeLimit = '"+CTMM_AgeLimit+"' , " +
                "CTMM_CriticalCareMedicine = '"+CTMM_CriticalCareMedicine+"' , " +
                "CTMM_AntibacterialSign = '"+CTMM_AntibacterialSign+"' , " +
                "CTMM_StatusCode = '"+CTMM_StatusCode+"' , " +
                "CTMM_StartDate = '"+CTMM_StartDate+"' , " +
                "CTMM_EndDate = '"+CTMM_EndDate+"' , " +
                "CTMM_DosUom = '"+CTMM_DosUom+"' , " +
                "CTMM_Dosage = '"+CTMM_Dosage+"' , " +
                "CTMM_MinPkgNum = '"+CTMM_MinPkgNum+"'  " +
                "CTMM_Code = '"+CTMM_Code+"'";

        System.out.println(data);

谢谢大家 不喜勿喷

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值