HI我是Java的新手,嘗試讀取XML文件。 這裏是我的XML文件: -使用java從複雜的xml中讀取值
<?xml version="1.0" encoding="UTF-8"?>
<parameter>
<attribute>a</attribute>
這裏是我的代碼,我想讀從XML密鑰和價值,但我堅持。這裏是我的代碼: -
public class TestDBMain {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
File file = new File("ACL.xml");
DocumentBuilderFactory dbfactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = dbfactory.newDocumentBuilder();
Document doc = builder.parse(file);
NodeList nList = doc.getElementsByTagName("testCaseDataName");
for(int i = 0;i<nList.getLength();i++){
Node nNode = nList.item(i);
if(nNode.getNodeType()== Node.ELEMENT_NODE){
Element ele = (Element) nNode;
// System.out.println(ele.getTextContent());
//System.out.println(ele.getElementsByTagName("testCaseName").item(0).getTextContent());
System.out.println(ele.getAttributeNode("testCaseDataName"));
//I dont know which methods to use to print the key and value in the xml under parameter
}
}
}
}
燦任何人請幫我這個
嘗試使用jaxb上下文 –