xml格式如下:
<?xml version="1.0" encoding="GBK" ?>
<FormElements>
<Elements>
<lable type="lable" name="boln" id="boln" value="提运单号:"></lable>
<lable type="lable" name="cszh" id="cszh" value="客户单证号:"></lable>
</Elements>
</FormElements>如何获取lable标签里面的type、name、id、value属性的值?
<?xml version="1.0" encoding="GBK" ?>
<FormElements>
<Elements>
<lable type="lable" name="boln" id="boln" value="提运单号:"></lable>
<lable type="lable" name="cszh" id="cszh" value="客户单证号:"></lable>
</Elements>
</FormElements>如何获取lable标签里面的type、name、id、value属性的值?
public static void main(String[] args) {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db;
Document doc;
try {
db = dbf.newDocumentBuilder();
doc = db.parse("d:/test.xml");
// 得到根节点
Element root = doc.getDocumentElement();
//得到Elements节点
NodeList nl = root.getElementsByTagName("Elements");
for (int i = 0; i < nl.getLength(); i++) {
Element e = (Element) nl.item(i);
NodeList child =e.getElementsByTagName("lable");
for(int j=0;j<child.getLength();j++) {
Element e1 = (Element) child.item(j);
System.out.print("type = "+e1.getAttribute("type")+" ");
System.out.print("name = "+e1.getAttribute("name")+" ");
System.out.print("id = "+e1.getAttribute("id")+" ");
System.out.println("value = "+e1.getAttribute("value"));
}
} } catch (ParserConfigurationException e) { } catch (SAXException e) { } catch (IOException e) { }
}