如何比较groovy/java解
hello world
test@163。com
2010-11-01 10:10:30
hello world demo 2
test@2163。com
2010-11-02 10:10:30
hello world demo 3
test3@163。 com
2010-11-03 10:10:30
java解析实现:
import org。w3c。dom。Document;
import org。w3c。dom。Node;
import org。 w3c。dom。NodeList;
import org。xml。sax。SAXException;
import javax。xml。...全部
hello world
test@163。com
2010-11-01 10:10:30
hello world demo 2
test@2163。com
2010-11-02 10:10:30
hello world demo 3
test3@163。
com
2010-11-03 10:10:30
java解析实现:
import org。w3c。dom。Document;
import org。w3c。dom。Node;
import org。
w3c。dom。NodeList;
import org。xml。sax。SAXException;
import javax。xml。parsers。DocumentBuilder;
import javax。
xml。parsers。DocumentBuilderFactory;
import javax。xml。parsers。ParserConfigurationException;
import java。
io。IOException;
import java。io。InputStream;
public class EmailsDemo {
public static void main(String[] args)
{
DocumentBuilderFactory factory = DocumentBuilderFactory。
newInstance();
try {
DocumentBuilder builder = factory。newDocumentBuilder();
InputStream is = EmailsDemo。
class。getResourceAsStream(“。/emails。xml”);
Document document = builder。parse(is);
NodeList nodes = document。
getElementsByTagName(“email”);
for (int i = 0; i < nodes。getLength(); i++)
{
Node node = nodes。item(i);
Node id = node。
getAttributes()。getNamedItem(“id”);
System。out。println(“id = ” + id。getTextContent());
}
} catch (ParserConfigurationException e) {
e。
printStackTrace();
} catch (SAXException e) {
e。printStackTrace();
} catch (IOException e) {
e。printStackTrace();
}
}
}
groovy解析实现:
def emails = new XmlParser()。
parse(“emails。xml”)
emails。email。each {
println “${it。@id} ${it。subject。text()} ”
}
可以看到groovy解析代码之少,实现之简单。
收起