xml dom 解析如何读取tag属性
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<definitions xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.ws-challenge.org/WSC08Services/">
<message name="serv2113336387RequestMessage">
</message>
</definitions>
请问给位大神这里面红字部分中等号后面的“serv2113336387RequestMessage”怎么才能读出来呢 ? 用getNodeValue 和 getNodeType 都得不到代码如下 加了 getFirstChild()加不加都出不来
代码如下File f=new File ("test/cc.xml");
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(f);
NodeList nl = doc.getElementsByTagName("service");
for (int i = 0; i < nl.getLength(); i++){
NodeList x= doc.getElementsByTagName("message");
for( int j=0;j<x.getLength();j++)
{
System.out.println("message name:"+doc.getElementsByTagName("message").item(j).(getFirstChild()).getNodeValue());
}
------最佳解决方案--------------------逻辑整错了吧?
前略...
Document doc = builder.parse(f);
NodeList x= doc.getElementsByTagName("message");
for (int j=0; j<x.getLength(); j++) {
System.out.println("message name:" + x.item(j));
Element e = (Element) x.item(j);
System.out.println("attribute:" + e.getAttribute("name"));
}