[英]get child elements and their values from selected XML element in java
Bit of background: 背景知识:
saveFilePath
is a location already selected by a user via a JFileChooser saveFilePath
是用户已经通过JFileChooser选择的位置
selectedElement
is a String which is the value selected from a comboBox selectedElement
是一个字符串,它是从comboBox中选择的值
NodeList selectedElementList = doc.getElementsByTagName(selectedElement);
returns all of the elements that are found in the XML file that match the element selected from the comboBox. 返回在XML文件中找到的与从comboBox中选择的元素匹配的所有元素。
What I'm trying to do is to get all child elements from the selected element, eg get elements A & B 我想做的是从所选元素中获取所有子元素,例如,获取元素A和B
<selectedElement>
<a> AA </a>
<b> BB </b>
</selectedElement>
<selectedElement>
<a> AB </a>
<b> BC </b>
</selectedElement>
Then find all instances of the selectedElement
and all the child elements associated with it to print out to a file. 然后找到
selectedElement
所有实例以及与之关联的所有子元素,以打印到文件中。 NOTE that selectedElement
is more than likely going to be a child of a child of the documents root element. 注意,
selectedElement
很有可能是documents根元素的子元素。
Here's what I've got so far, the fileWriter.append(a + "," + + newline);
到目前为止,这是我得到的,
fileWriter.append(a + "," + + newline);
prints out all of the occurrences of the selected elements within the XML file, but I can't seem to figure out how to get it to print out the child Elements and their values. 打印出XML文件中所有选定元素的出现,但我似乎无法弄清楚如何获取它来打印子元素及其值。
This is what I have: 这就是我所拥有的:
try {
// Make a file writer that uses the location entered
fileWriter = new FileWriter(saveFilePath);
fileWriter.append(selectedElement + "," + newline);
// For each occurance of the element
for (int k = 0; k < selectedElementList.getLength(); k++) {
// Add the value of it to the CSV file
String a = selectedElementList.item(k).getTextContent();
fileWriter.append(a + "," + newline);
// for (int l = 0; l < a.getLength(); l++) {
//
//
// String item = element.getNodeName().toString();
// String item2 = element.getNodeValue();
//
// System.out.println("The node name is: " + item + newline);
// System.out.println("The node VALUE is: " + item2 + newline);
// }
}
// Close the fileWriter stream
fileWriter.flush();
fileWriter.close();
// Disable the create button and enable the open button
createButton.setEnabled(false);
openButton.setEnabled(true);
} catch (IOException e) {
e.printStackTrace();
}
Maybe I need to create a second document that contains only the required XML? 也许我需要创建仅包含所需XML的第二个文档? or maybe getElementsByTagName isn't the right method to be using?
还是getElementsByTagName不是正确的方法? I basicalla want to get the child nodes of the selected element!
我basicalla想获取所选元素的子节点!
Any help would be appreciated 任何帮助,将不胜感激
I'd post the whole thing on here but it's 320 lines long and this is just one of the many parts of my little application :) 我将整个内容发布在这里,但是它有320行,这只是我的小应用程序很多部分之一:)
I think I have a solution using something called "dynamic data projection " (Disclosure: I'm affiliated with that project). 我认为我有一个使用“动态数据投影 ”的解决方案(披露:我隶属于该项目)。 But I changed your XML to make sense (added root element
and gave the selected elements different names so I can select one of them
). 但是我改变了XML的含义(添加了root元素
,并为所选元素指定了不同的名称,以便我可以选择其中之一
)。 Here is my XML version: 这是我的XML版本:
<root>
<selectedElement>
<a> AA </a>
<b> BB </b>
</selectedElement>
<selectedElement>
<a> AB </a>
<b> BC </b>
</selectedElement>
</root>
And here is the program: 这是程序:
public class ExtractElement {
public interface Projection {
interface SelectedElement {
@XBRead("name()")
String getName();
@XBRead(".")
String getValue();
}
@XBRead("//{0}/*")
List<SelectedElement> getSelectedElements(String name);
}
public static void main(String[] args) throws IOException {
Projection projection = new XBProjector().io().url("resource://data.xml").read(Projection.class);
for (SelectedElement se:projection.getSelectedElements("selectedElement")) {
System.out.println("Found element with name "+se.getName()+" and value "+se.getValue());
}
}
}
It prints out: 它输出:
Found element with name a and value AA
Found element with name b and value BB
Found element with name a and value AB
Found element with name b and value BC
Is this what you liked to have? 这就是您想要的东西吗?
Managed to solve this by doing the following: 通过执行以下操作设法解决了这个问题:
// For each occurance of the element
for (int k = 0; k < selectedElementList.getLength(); k++) {
// Add the value of it to the CSV file
String a = selectedElementList.item(k).getTextContent();
fileWriter.append(a + "," + newline);
//log.append(a + "," + newline);
NodeList temp = selectedElementList.item(k).getChildNodes();
for(int l = 0; l < temp.getLength(); l++) {
Node tempNode = temp.item(l);
if (tempNode.hasChildNodes()) {
String tnodeName = tempNode.getNodeName();
log.append("tNodeName is: " + tnodeName.toString() + newline);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.