欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Java解析Xml文件—判断Xml文件的节点是否存在子节点_以及对节点下不同子节点的内容解析方式

程序员文章站 2022-05-02 11:41:19
...

原文链接:https://blog.csdn.net/lwt_shine/article/details/44623471

XML文件解析

XML源码

判断Xml文件中某一个节点是否存在子节点:

首先要获取到节点的document,
Element root = document.getRootElement();
Element HotelGeoList = root.element(“HotelGeoList”);
for(Iterator iteratorHotelGeo = HotelGeoList.elementIterator(“HotelGeo”);iteratorHotelGeo.hasNext()????{
Element HotelGeo = (Element) iteratorHotelGeo.next();
Document d = HotelGeo.getDocument();
if(d.selectNodes("//*").size()>0){
System.out.println(“存在子节点”);
}

节点下不同子节点内容的解析

Element District = HotelGeo.element(“Districts”);
if(District!=null){ //判断是否存在district的子节点
for(Iterator iteratorLocation = District.elementIterator(“Location”);iteratorLocation .hasNext()????{
District district = new District();
Element Locations = (Element) iteratorLocation.next();
district.setDistrictName(Locations.attributeValue(“Name”));
district.setDistrictId(Locations.attributeValue(“Id”));
district.setCity(city);
district = districtService.createDistrict(district);
System.out.println("---------------"+district.getDistrictId()+"–"+district.getDistrictName());
}
}

Element CommericalLocation = HotelGeo.element(“CommericalLocations”);
if(CommericalLocation!=null){//判断是否存在CommericalLocation子节点

    for(Iterator iteratorLocation  = CommericalLocation.elementIterator("Location");iteratorLocation .hasNext();){
        CommericalLocation commericalLocation = new CommericalLocation();
        Element Locations = (Element) iteratorLocation.next();
        commericalLocation.setCommericalLocationName(Locations.attributeValue("Name"));
        commericalLocation.setCommericalLocationId(Locations.attributeValue("Id"));
        commericalLocation.setCity(city);
        commericalLocation = commericalLocationService.createCommericalLocation(commericalLocation);
        System.out.println("---------------"+commericalLocation.getCommericalLocationId()+"-----"+commericalLocation.getCommericalLocationName());
    }
  }

Element LandmarkLocation = HotelGeo.element(“LandmarkLocations”);
if(LandmarkLocation!=null){//判断是否存在LandmarkLocation子节点

     for(Iterator iteratorLocation  = LandmarkLocation.elementIterator("Location");iteratorLocation .hasNext();){
        LandmarkLocation landmarkLocation = new LandmarkLocation();
        Element Locations = (Element) iteratorLocation.next();
        landmarkLocation.setLandmarkLocationName(Locations.attributeValue("Name"));
        landmarkLocation.setLandmarkLocationId(Locations.attributeValue("Id"));
        landmarkLocation.setCity(city);
        landmarkLocation = landmarkLocationService.createLandmarkLocation(landmarkLocation);
        System.out.println("---------------"+landmarkLocation.getLandmarkLocationId()+"---"+landmarkLocation.getLandmarkLocationName());
     }
}

————————————————
版权声明:本文为CSDN博主「Never_Nice」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/lwt_shine/article/details/44623471

相关标签: 后端开发