几天来,我一直在研究 XML 解析器,并且main
大部分时间都在为整个项目工作。代码开始变得混乱,我有一些问题。
// Initializes the xPath objects for XML parsing use
XPathFactory xPathfactory = XPathFactory.newInstance();
XPath xpath = xPathfactory.newXPath();
XPathExpression hourly = xpath.compile("/dwml/data/time-layout[3]/start-valid-time/text()"); // Time tri-hourly. Parses in the third time-layout, as this is the time information we need
XPathExpression tempHourly = xpath.compile("/dwml/data/parameters/temperature[@type='hourly']/value/text()"); // Temperature tri-hourly. Parses in through the 'hourly' temperature attribute
XPathExpression dewPoint = xpath.compile("/dwml/data/parameters/temperature[@type='dew point']/value/text()"); // Dew point tri-hourly. Parses in through the 'dew point' temperature attribute
XPathExpression windSpeed = xpath.compile("/dwml/data/parameters/wind-speed[@type='sustained']/value/text()"); // Sustained wind speed tri-hourly. Parses in through the 'sustained' wind-speed attribute
XPathExpression relHum = xpath.compile("/dwml/data/parameters/humidity[@type='relative']/value/text()"); // Relative humidity tri-hourly. Parses in through the 'relative' humidity attribute
// Casting the objects to NodeLists
NodeList hourlyResult = (NodeList) hourly.evaluate(doc,XPathConstants.NODESET);
NodeList tempHourlyResult = (NodeList) tempHourly.evaluate(doc,XPathConstants.NODESET);
NodeList dewPointResult = (NodeList) dewPoint.evaluate(doc,XPathConstants.NODESET);
NodeList windSpeedResult = (NodeList) windSpeed.evaluate(doc,XPathConstants.NODESET);
NodeList relHumResult = (NodeList) relHum.evaluate(doc,XPathConstants.NODESET);
在 main 工作时,我不担心面向对象的编程,但是,我真的很想将这些更改为static
orpublic
变量。有人可以告诉我正确的方法,面向对象的风格吗?