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();
}
原文链接:https://www.f2er.com/xml/294889.html