我的 Xml 文件如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE pointList SYSTEM "point.dtd">
<pointList>
<point unit="mm">
<x>2</x>
<y>3</y>
</point>
<point unit="cm">
<x>9</x>
<y>3</y>
</point>
<point unit="px">
<x>4</x>
<y>7</y>
</point>
</pointList>
当我尝试获取标签点的属性时:
import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class TryXml {
public static void main (String [] args) {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = null;
Document doc = null;
try {
builder = factory.newDocumentBuilder();
}
catch (ParserConfigurationException e){
System.out.println(e.getMessage());
}
File f = new File("p1.xml");
try {
doc=builder.parse(f);
}
catch (SAXException e){
e.printStackTrace();
}
catch (IOException e){
e.printStackTrace();
}
Element root = doc.getDocumentElement();
System.out.println(root.getTagName());
System.out.println("****************");
NodeList nList = root.getChildNodes();
for (int i=0;i<nList.getLength();i++) {
if(nList.item(i) instanceof Element)
System.out.println(nList.item(i).getAttributes());
}
}
}
我得到的只是地址:
com.sun.org.apache.xerces.internal.dom.AttributeMap@3214512e
com.sun.org.apache.xerces.internal.dom.AttributeMap@53ddbcb1
com.sun.org.apache.xerces.internal.dom.AttributeMap@28f337b
谁能给我一个关于如何获取点属性以及其他内部标签的提示?