xml在java中的运用

前端之家收集整理的这篇文章主要介绍了xml在java中的运用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
xml类型的String,转换为doc进行数据的解析操作
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(new InputSource(new StringReader(protocolXML)));

Element root = doc.getDocumentElement();
NodeList outs = root.getChildNodes();
if (outs != null) {
for (int i = 0; i < outs.getLength(); i++) {
Node out = outs.item(i);
if ("FILE_TYPE".equals(out.getNodeName())) {
String filetype = out.getFirstChild().getNodeValue();
System.out.println(filetype);
}
if ("RECORD".equals(out.getNodeName())) {
NodeList records = out.getChildNodes();
if (records != null) {
for (int j = 0; j < records.getLength(); j++) {
Node record = records.item(j);
if ("REF_NO".equals(record.getNodeName())) {
String refno = record.getFirstChild().getNodeValue();
System.out.println(refno);
}

}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}

猜你在找的XML相关文章