Java 读取 XML 文件内容

  • XML文件内容
    <?xml version="1.0" encoding="UTF-8"?>
    <ip_type>
        <ip name="办公网">192.168.75.167</ip>
        <ip name="研发网">192.168.102.168</ip>
    </ip_type>
    
  • Java读取XML示例代码
    import java.io.File;
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.List;
    import java.util.Map;
    
    import org.dom4j.Document;
    import org.dom4j.DocumentException;
    import org.dom4j.Element;
    import org.dom4j.io.SAXReader;
    
    public class NetworkUtil {    
        public static Map<String, String> network_map = new HashMap<String, String>();
            
        private NetworkUtil() {
            //读取XML文件,获得document对象
            SAXReader reader = new SAXReader();
            Document document = null;
            try {
                document = reader.read(new File("./network.xml"));
            } catch (DocumentException e) {
                e.printStackTrace();
            }
    
            Element node = document.getRootElement();//获取根节点
            List node1 = node.elements("ip");//获取根节点下所有元素名为“ip”的元素
            Map<String, String> map = new HashMap<String, String>();//汉字作为key,ip地址作为value
    
            for (Iterator it = node1.iterator(); it.hasNext();) {
                Element elm = (Element) it.next();
                map.put(elm.attributeValue("name"), elm.getText());
            }
            network_map = map;
        }
    
        private static final NetworkUtil networkUtil = new NetworkUtil(); 
            
        public static NetworkUtil getInstance(){        
            return networkUtil;
        }
    }
    
已标记关键词 清除标记
<h3>     java解析xml文件多个线程同时访问时内存溢出,代码如下:</h3> <p><strong><span style="white-space: pre;"> </span>解析的xml文件大小大概2M-10M,该怎么解决呢?</strong></p> <p> </p> <pre name="code" class="java">String sourceFile = ConfigReader.get("data.store.path") + article.getSourceFile(); org.dom4j.Document document = null; FileInputStream fin = null; try { fin = new FileInputStream(new File(sourceFile)); document = new SAXReader().read(fin); } catch (final FileNotFoundException e) { final String msg = "内容单元xml文件不存在:" + sourceFile; log.error(msg, e); throw new RuntimeException(msg, e); } catch (final DocumentException e) { final String msg = "解析内容单元xml文件失败:" + sourceFile; log.error(msg, e); throw new RuntimeException(msg, e); } finally { IOUtils.closeQuietly(fin); } final StringBuilder xpath = new StringBuilder("/KFMP/DOCS/DOC[@GUID='") .append(article.getGuid()).append("']/").append("CONTENT"); final Node node = document.selectSingleNode(xpath.toString()); final String value = node.getText(); article.setContent(value); final StringBuilder coordXpath = new StringBuilder("/KFMP/DOCS/DOC[@GUID='") .append(article.getGuid()).append("']/").append("COORDS"); final Node coordsNode = document.selectSingleNode(coordXpath.toString()); final String coordsValue = coordsNode.getText(); article.setCoords(coordsValue); final StringBuilder briefXpath = new StringBuilder("/KFMP/DOCS/DOC[@GUID='") .append(article.getGuid()).append("']/").append("BRIEF"); final Node briefNode = document.selectSingleNode(briefXpath.toString()); final String briefValue = briefNode.getText(); article.setBrief(briefValue); return article;</pre>
xml文件是遥感数据的的头文件,希望从中提取时间区域等信息,Java小白,求大佬们教教怎么整 ,头文件如下: ``` <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE GranuleMetaDataFile SYSTEM "http://ecsinfo.gsfc.nasa.gov/ECSInfo/ecsmetadata/dtds/DPL/ECS/ScienceGranuleMetadata.dtd"> <GranuleMetaDataFile> <DTDVersion>1.0</DTDVersion> <DataCenterId>EDC</DataCenterId> <GranuleURMetaData> <GranuleUR>SC:MOD13A2.005:2132545364</GranuleUR> <DbID>2132545364</DbID> <InsertTime>2014-06-11 02:11:28.496</InsertTime> <LastUpdate>2014-06-12 02:11:37.037</LastUpdate> <CollectionMetaData> <ShortName>MOD13A2</ShortName> <VersionID>5</VersionID> </CollectionMetaData> <DataFiles> <DataFileContainer> <DistributedFileName>MOD13A2.A2014145.h27v05.005.2014162030850.hdf</DistributedFileName> <FileSize>16891955</FileSize> <ChecksumType>CKSUM</ChecksumType> <Checksum>1091827715</Checksum> <ChecksumOrigin>DPLIngst</ChecksumOrigin> </DataFileContainer> </DataFiles> <ECSDataGranule> <SizeMBECSDataGranule>16.1094</SizeMBECSDataGranule> <ReprocessingPlanned>further update is anticipated</ReprocessingPlanned> <ReprocessingActual>reprocessed</ReprocessingActual> <LocalGranuleID>MOD13A2.A2014145.h27v05.005.2014162030850.hdf</LocalGranuleID> <DayNightFlag>Day</DayNightFlag> <ProductionDateTime>2014-06-11 07:08:50.000</ProductionDateTime> <LocalVersionID>5.2.2</LocalVersionID> </ECSDataGranule> <PGEVersionClass> <PGEVersion>5.2.7</PGEVersion> </PGEVersionClass> <RangeDateTime> <RangeEndingTime>23:59:59.000000</RangeEndingTime> <RangeEndingDate>2014-06-09</RangeEndingDate> <RangeBeginningTime>00:00:00.000000</RangeBeginningTime> <RangeBeginningDate>2014-05-25</RangeBeginningDate> </RangeDateTime> <SpatialDomainContainer> <HorizontalSpatialDomainContainer> <GPolygon> <Boundary> <Point> <PointLongitude>103.700805509875</PointLongitude> <PointLatitude>29.9063240058583</PointLatitude> </Point> <Point> <PointLongitude>117.356422833073</PointLongitude> <PointLatitude>40.0849484191919</PointLatitude> </Point> <Point> <PointLongitude>130.564483119946</PointLongitude> <PointLatitude>39.998491208368</PointLatitude> </Point> <Point> <PointLongitude>115.388076442344</PointLongitude> <PointLatitude>29.8298156547937</PointLatitude> </Point> </Boundary> </GPolygon> </HorizontalSpatialDomainContainer> </SpatialDomainContainer> <MeasuredParameter> <MeasuredParameterContainer> <ParameterName>1 km 16 days NDVI</ParameterName> <QAStats> <QAPercentMissingData>0</QAPercentMissingData> <QAPercentOutofBoundsData>0</QAPercentOutofBoundsData> <QAPercentInterpolatedData>100</QAPercentInterpolatedData> <QAPercentCloudCover>2</QAPercentCloudCover> </QAStats> <QAFlags> <AutomaticQualityFlag>Passed</AutomaticQualityFlag> <AutomaticQualityFlagExplanation>No automatic quality assessment is performed in the PGE</AutomaticQualityFlagExplanation> <OperationalQualityFlag>Passed</OperationalQualityFlag> <OperationalQualityFlagExplanation>Passed</OperationalQualityFlagExplanation> <ScienceQualityFlag>Inferred Passed</ScienceQualityFlag> <ScienceQualityFlagExplanation>http://landweb.nascom.nasa.gov/cgi-bin/QA_WWW/detailInfo.cgi?prod_id=MOD13A2&ver=C5.2014-06-12 02:11</ScienceQualityFlagExplanation> </QAFlags> </MeasuredParameterContainer> <MeasuredParameterContainer> <ParameterName>1 km 16 days EVI</ParameterName> <QAStats> <QAPercentMissingData>0</QAPercentMissingData> <QAPercentOutofBoundsData>0</QAPercentOutofBoundsData> <QAPercentInterpolatedData>100</QAPercentInterpolatedData> <QAPercentCloudCover>2</QAPercentCloudCover> </QAStats> <QAFlags> <AutomaticQualityFlag>Passed</AutomaticQualityFlag> <AutomaticQualityFlagExplanation>No automatic quality assessment is performed in the PGE</AutomaticQualityFlagExplanation> <ScienceQualityFlag>Inferred Passed</ScienceQualityFlag> <ScienceQualityFlagExplanation>http://landweb.nascom.nasa.gov/cgi-bin/QA_WWW/detailInfo.cgi?prod_id=MOD13A2&ver=C5.2014-06-12 02:11</ScienceQualityFlagExplanation> </QAFlags> </MeasuredParameterContainer> <MeasuredParameterContainer> <ParameterName>1 km 16 days VI Quality</ParameterName> <QAStats> <QAPercentMissingData>0</QAPercentMissingData> <QAPercentOutofBoundsData>0</QAPercentOutofBoundsData> <QAPercentInterpolatedData>100</QAPercentInterpolatedData> <QAPercentCloudCover>2</QAPercentCloudCover> </QAStats> <QAFlags> <AutomaticQualityFlag>Passed</AutomaticQualityFlag> <AutomaticQualityFlagExplanation>No automatic quality assessment is performed in the PGE</AutomaticQualityFlagExplanation> <ScienceQualityFlag>Inferred Passed</ScienceQualityFlag> <ScienceQualityFlagExplanation>http://landweb.nascom.nasa.gov/cgi-bin/QA_WWW/detailInfo.cgi?prod_id=MOD13A2&ver=C5.2014-06-12 02:11</ScienceQualityFlagExplanation> </QAFlags> </MeasuredParameterContainer> <MeasuredParameterContainer> <ParameterName>1 km 16 days red reflectance</ParameterName> <QAStats> <QAPercentMissingData>0</QAPercentMissingData> <QAPercentOutofBoundsData>0</QAPercentOutofBoundsData> <QAPercentInterpolatedData>100</QAPercentInterpolatedData> <QAPercentCloudCover>2</QAPercentCloudCover> </QAStats> <QAFlags> <AutomaticQualityFlag>Passed</AutomaticQualityFlag> <AutomaticQualityFlagExplanation>No automatic quality assessment is performed in the PGE</AutomaticQualityFlagExplanation> <ScienceQualityFlag>Inferred Passed</ScienceQualityFlag> <ScienceQualityFlagExplanation>http://landweb.nascom.nasa.gov/cgi-bin/QA_WWW/detailInfo.cgi?prod_id=MOD13A2&ver=C5.2014-06-12 02:11</ScienceQualityFlagExplanation> </QAFlags> </MeasuredParameterContainer> <MeasuredParameterContainer> <ParameterName>1 km 16 days NIR reflectance</ParameterName> <QAStats> <QAPercentMissingData>0</QAPercentMissingData> <QAPercentOutofBoundsData>0</QAPercentOutofBoundsData> <QAPercentInterpolatedData>100</QAPercentInterpolatedData> <QAPercentCloudCover>2</QAPercentCloudCover> </QAStats> <QAFlags> <AutomaticQualityFlag>Passed</AutomaticQualityFlag> <AutomaticQualityFlagExplanation>No automatic quality assessment is performed in the PGE</AutomaticQualityFlagExplanation> <ScienceQualityFlag>Inferred Passed</ScienceQualityFlag> <ScienceQualityFlagExplanation>http://landweb.nascom.nasa.gov/cgi-bin/QA_WWW/detailInfo.cgi?prod_id=MOD13A2&ver=C5.2014-06-12 02:11</ScienceQualityFlagExplanation> </QAFlags> </MeasuredParameterContainer> <MeasuredParameterContainer> <ParameterName>1 km 16 days blue reflectance</ParameterName> <QAStats> <QAPercentMissingData>0</QAPercentMissingData> <QAPercentOutofBoundsData>0</QAPercentOutofBoundsData> <QAPercentInterpolatedData>100</QAPercentInterpolatedData> <QAPercentCloudCover>2</QAPercentCloudCover> </QAStats> <QAFlags> <AutomaticQualityFlag>Passed</AutomaticQualityFlag> <AutomaticQualityFlagExplanation>No automatic quality assessment is performed in the PGE</AutomaticQualityFlagExplanation> <ScienceQualityFlag>Inferred Passed</ScienceQualityFlag> <ScienceQualityFlagExplanation>http://landweb.nascom.nasa.gov/cgi-bin/QA_WWW/detailInfo.cgi?prod_id=MOD13A2&ver=C5.2014-06-12 02:11</ScienceQualityFlagExplanation> </QAFlags> </MeasuredParameterContainer> <MeasuredParameterContainer> <ParameterName>1 km 16 days MIR reflectance</ParameterName> <QAStats> <QAPercentMissingData>0</QAPercentMissingData> <QAPercentOutofBoundsData>0</QAPercentOutofBoundsData> <QAPercentInterpolatedData>100</QAPercentInterpolatedData> <QAPercentCloudCover>2</QAPercentCloudCover> </QAStats> <QAFlags> <AutomaticQualityFlag>Passed</AutomaticQualityFlag> <AutomaticQualityFlagExplanation>No automatic quality assessment is performed in the PGE</AutomaticQualityFlagExplanation> <ScienceQualityFlag>Inferred Passed</ScienceQualityFlag> <ScienceQualityFlagExplanation>http://landweb.nascom.nasa.gov/cgi-bin/QA_WWW/detailInfo.cgi?prod_id=MOD13A2&ver=C5.2014-06-12 02:11</ScienceQualityFlagExplanation> </QAFlags> </MeasuredParameterContainer> <MeasuredParameterContainer> <ParameterName>1 km 16 days view zenith angle</ParameterName> <QAStats> <QAPercentMissingData>0</QAPercentMissingData> <QAPercentOutofBoundsData>0</QAPercentOutofBoundsData> <QAPercentInterpolatedData>100</QAPercentInterpolatedData> <QAPercentCloudCover>2</QAPercentCloudCover> </QAStats> <QAFlags> <AutomaticQualityFlag>Passed</AutomaticQualityFlag> <AutomaticQualityFlagExplanation>No automatic quality assessment is performed in the PGE</AutomaticQualityFlagExplanation> <ScienceQualityFlag>Inferred Passed</ScienceQualityFlag> <ScienceQualityFlagExplanation>http://landweb.nascom.nasa.gov/cgi-bin/QA_WWW/detailInfo.cgi?prod_id=MOD13A2&ver=C5.2014-06-12 02:11</ScienceQualityFlagExplanation> </QAFlags> </MeasuredParameterContainer> <MeasuredParameterContainer> <ParameterName>1 km 16 days sun zenith angle</ParameterName> <QAStats> <QAPercentMissingData>0</QAPercentMissingData> <QAPercentOutofBoundsData>0</QAPercentOutofBoundsData> <QAPercentInterpolatedData>100</QAPercentInterpolatedData> <QAPercentCloudCover>2</QAPercentCloudCover> </QAStats> <QAFlags> <AutomaticQualityFlag>Passed</AutomaticQualityFlag> <AutomaticQualityFlagExplanation>No automatic quality assessment is performed in the PGE</AutomaticQualityFlagExplanation> <ScienceQualityFlag>Inferred Passed</ScienceQualityFlag> <ScienceQualityFlagExplanation>http://landweb.nascom.nasa.gov/cgi-bin/QA_WWW/detailInfo.cgi?prod_id=MOD13A2&ver=C5.2014-06-12 02:11</ScienceQualityFlagExplanation> </QAFlags> </MeasuredParameterContainer> <MeasuredParameterContainer> <ParameterName>1 km 16 days relative azimuth angle</ParameterName> <QAStats> <QAPercentMissingData>0</QAPercentMissingData> <QAPercentOutofBoundsData>0</QAPercentOutofBoundsData> <QAPercentInterpolatedData>100</QAPercentInterpolatedData> <QAPercentCloudCover>2</QAPercentCloudCover> </QAStats> <QAFlags> <AutomaticQualityFlag>Passed</AutomaticQualityFlag> <AutomaticQualityFlagExplanation>No automatic quality assessment is performed in the PGE</AutomaticQualityFlagExplanation> <ScienceQualityFlag>Inferred Passed</ScienceQualityFlag> <ScienceQualityFlagExplanation>http://landweb.nascom.nasa.gov/cgi-bin/QA_WWW/detailInfo.cgi?prod_id=MOD13A2&ver=C5.2014-06-12 02:11</ScienceQualityFlagExplanation> </QAFlags> </MeasuredParameterContainer> <MeasuredParameterContainer> <ParameterName>1 km 16 days composite day of the year</ParameterName> <QAStats> <QAPercentMissingData>0</QAPercentMissingData> <QAPercentOutofBoundsData>0</QAPercentOutofBoundsData> <QAPercentInterpolatedData>100</QAPercentInterpolatedData> <QAPercentCloudCover>2</QAPercentCloudCover> </QAStats> <QAFlags> <AutomaticQualityFlag>Passed</AutomaticQualityFlag> <AutomaticQualityFlagExplanation>No automatic quality assessment is performed in the PGE</AutomaticQualityFlagExplanation> <ScienceQualityFlag>Inferred Passed</ScienceQualityFlag> <ScienceQualityFlagExplanation>http://landweb.nascom.nasa.gov/cgi-bin/QA_WWW/detailInfo.cgi?prod_id=MOD13A2&ver=C5.2014-06-12 02:11</ScienceQualityFlagExplanation> </QAFlags> </MeasuredParameterContainer> <MeasuredParameterContainer> <ParameterName>1 km 16 days pixel reliability</ParameterName> <QAStats> <QAPercentMissingData>0</QAPercentMissingData> <QAPercentOutofBoundsData>0</QAPercentOutofBoundsData> <QAPercentInterpolatedData>100</QAPercentInterpolatedData> <QAPercentCloudCover>2</QAPercentCloudCover> </QAStats> <QAFlags> <AutomaticQualityFlag>Passed</AutomaticQualityFlag> <AutomaticQualityFlagExplanation>No automatic quality assessment is performed in the PGE</AutomaticQualityFlagExplanation> <ScienceQualityFlag>Inferred Passed</ScienceQualityFlag> <ScienceQualityFlagExplanation>http://landweb.nascom.nasa.gov/cgi-bin/QA_WWW/detailInfo.cgi?prod_id=MOD13A2&ver=C5.2014-06-12 02:11</ScienceQualityFlagExplanation> </QAFlags> </MeasuredParameterContainer> </MeasuredParameter> <Platform> <PlatformShortName>Terra</PlatformShortName> <Instrument> <InstrumentShortName>MODIS</InstrumentShortName> <Sensor> <SensorShortName>MODIS</SensorShortName> </Sensor> </Instrument> </Platform> <PSAs> <PSA> <PSAName>QAPERCENTGOODQUALITY</PSAName> <PSAValue>57</PSAValue> </PSA> <PSA> <PSAName>QAPERCENTOTHERQUALITY</PSAName> <PSAValue>43</PSAValue> </PSA> <PSA> <PSAName>QAPERCENTNOTPRODUCEDCLOUD</PSAName> <PSAValue>0</PSAValue> </PSA> <PSA> <PSAName>QAPERCENTNOTPRODUCEDOTHER</PSAName> <PSAValue>0</PSAValue> </PSA> <PSA> <PSAName>NDVI1KM16DAYQCLASSPERCENTAGE</PSAName> <PSAValue>0</PSAValue> </PSA> <PSA> <PSAName>EVI1KM16DAYQCLASSPERCENTAGE</PSAName> <PSAValue>0</PSAValue> </PSA> <PSA> <PSAName>HORIZONTALTILENUMBER</PSAName> <PSAValue>27</PSAValue> </PSA> <PSA> <PSAName>VERTICALTILENUMBER</PSAName> <PSAValue>05</PSAValue> </PSA> <PSA> <PSAName>TileID</PSAName> <PSAValue>51027005</PSAValue> </PSA> </PSAs> <InputGranule> <InputPointer>MODAGAGG.A2014145.h27v05.005.2014147020023.hdf</InputPointer> <InputPointer>MODAGAGG.A2014146.h27v05.005.2014148040040.hdf</InputPointer> <InputPointer>MODAGAGG.A2014147.h27v05.005.2014148191855.hdf</InputPointer> <InputPointer>MODAGAGG.A2014148.h27v05.005.2014150002959.hdf</InputPointer> <InputPointer>MODAGAGG.A2014149.h27v05.005.2014151014416.hdf</InputPointer> <InputPointer>MODAGAGG.A2014150.h27v05.005.2014152015220.hdf</InputPointer> <InputPointer>MODAGAGG.A2014151.h27v05.005.2014152142901.hdf</InputPointer> <InputPointer>MODAGAGG.A2014152.h27v05.005.2014154031330.hdf</InputPointer> <InputPointer>MODAGAGG.A2014153.h27v05.005.2014155023253.hdf</InputPointer> <InputPointer>MODAGAGG.A2014154.h27v05.005.2014156015817.hdf</InputPointer> <InputPointer>MODAGAGG.A2014155.h27v05.005.2014157021913.hdf</InputPointer> <InputPointer>MODAGAGG.A2014156.h27v05.005.2014158022626.hdf</InputPointer> <InputPointer>MODAGAGG.A2014157.h27v05.005.2014159020319.hdf</InputPointer> <InputPointer>MODAGAGG.A2014158.h27v05.005.2014160022748.hdf</InputPointer> <InputPointer>MODAGAGG.A2014159.h27v05.005.2014161023954.hdf</InputPointer> <InputPointer>MODAGAGG.A2014160.h27v05.005.2014162014912.hdf</InputPointer> </InputGranule> <BrowseProduct> <BrowseGranuleId>UR:10:DsShESDTUR:UR:15:DsShSciServerUR:13:[EDC:DSSDSRV]:24:BR:Browse.001:2132545365</BrowseGranuleId> </BrowseProduct> </GranuleURMetaData> </GranuleMetaDataFile> ```
©️2020 CSDN 皮肤主题: 创作都市 设计师:CSDN官方博客 返回首页