使用java从复杂的xml中读取值
您好,我是Java新手,正在尝试读取XML文件。 这是我的XML文件:-使用java从复杂的xml中读取值,java,xml,Java,Xml,您好,我是Java新手,正在尝试读取XML文件。 这是我的XML文件:- <?xml version="1.0" encoding="UTF-8"?> <parameter> <attribute>a</attribute> A. 这是我的代码,我正试图从xml中读取密钥和值,但我被卡住了。这是我的代码:- public class TestDBMain { public static void main(String[
<?xml version="1.0" encoding="UTF-8"?>
<parameter>
<attribute>a</attribute>
A.
这是我的代码,我正试图从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
}
}
}
}
公共类TestDBMain{
公共静态void main(字符串[]args)引发异常{
//TODO自动生成的方法存根
File File=新文件(“ACL.xml”);
DocumentBuilderFactory dbfactory=DocumentBuilderFactory.newInstance();
DocumentBuilder=dbfactory.newDocumentBuilder();
文档doc=builder.parse(文件);
NodeList nList=doc.getElementsByTagName(“testCaseDataName”);
对于(int i=0;i您的代码将读取
节点。它不会进入此标记的内部。
所以试试这个
for(int i = 0;i<nList.getLength();i++){
NodeList nodeList = nList.item(i).getChildNodes();
for(int j = 0;j<nList.getLength();j++){
Node nNode = nodeList.item(j);
if(nNode.getNodeType()== Node.ELEMENT_NODE){
System.out.println(nNode.getNodeName() +" : "+nNode.getTextContent());
if(nNode.getNodeName().equals("parameter")){
NodeList param = nNode.getChildNodes();
System.out.println(" "+param.item(0).getNodeName() +" : "+param.item(0).getTextContent());
System.out.println(" "+param.item(1).getNodeName() +" : "+param.item(1).getTextContent());
}
}
}
}
for(inti=0;i免责声明:我维护了,所以我有偏见……但是……这是JDOM的理想用例:
Document doc = new SAXBuilder().build(new File("ACL.xml"));
Element root = doc.getRootElement();
for (Element testcase : root.getChildren()) {
int id = Integer.parseInt(testcase.getChildText("id"));
String name = testcase.getChildText("testCaseName");
String expect = testcase.getChildText("expectedResult");
Map<String,String> params = new LinkedHashMap<String,String>();
Element parmemt = testcase.getChild("parameter");
if (parmemt != null) {
Iterator<Element> it = parmemt.getChildren().iterator();
while (it.hasNext()) {
Element key = it.next();
if (!"key".equals(key.getName())) {
throw new IllegalStateException("Expected key but got " + key);
}
if (!it.hasNext()) {
throw new IllegalStateException("Expected value for key " + key);
}
Element val = it.next();
if (!"value".equals(val.getName())) {
throw new IllegalStateException("Expected value but got " + val);
}
params.put(key.getValue(), val.getValue());
}
}
System.out.printf("Processing test case %d -> %s\n Expect %s\n Parameters: %s\n",
id, name, expect, params.toString());
}
尝试使用jaxb上下文这只是打印id。我想打印键值标记See my ans…,但我要显式地告诉index。因此根据您的逻辑进行更改我下载二进制文件。我应该导入哪些jar?您只需要jdom-2.0.5.jar…并阅读以下内容:
Processing test case 1 -> EditTest
Expect nooptionsacltrue
Parameters: {}
Processing test case 2 -> AddTest
Expect featuresaddedacltrue
Parameters: {featues=w,f}
Processing test case 3 -> AddTest
Expect duplicateacltrue
Parameters: {projectType=NEW, Name=28HPM, status=ACTIVE, canOrder=Yes}