JAVA XML - 如何获取 XML 节点中的特定元素?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27031476/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JAVA XML - How do I get specific elements in an XML Node?
提问by boom_box
This is an academic assignment and we are given an extremely large XML file with hundreds of entries like these. For each item we are supposed to list the Manager's ID, the Person's ID of the last person to add an item to the list, and the current number of items. I have read and reread the Oracle DOM API and various Node APIs. We are using JAVA and I cannot for the life of me figure out how to search various 'fields' of each item_list
node. Below is an example of the data we are given.
这是一项学术作业,我们得到了一个非常大的 XML 文件,其中包含数百个这样的条目。对于每个项目,我们应该列出经理的 ID、最后一个将项目添加到列表中的人员的个人 ID,以及当前项目的数量。我已经阅读并重读了 Oracle DOM API 和各种 Node API。我们正在使用 JAVA,我一生都无法弄清楚如何搜索每个item_list
节点的各种“字段” 。下面是我们给出的数据的一个例子。
<item_list id="item_list01">
<numitems_intial>5</numitems_initial>
<item>
<date_added>1/1/2014</date_added>
<added_by person="person01" />
</item>
<item>
<date_added>1/6/2014</date_added>
<added_by person="person05" />
</item>
<numitems_current>7</numitems_current>
<manager person="person48" />
</item_list>
<item_list id="item_list02">
<numitems_intial>5</numitems_initial>
<item>
<date_added>1/15/2014</date_added>
<added_by person="person05" />
</item>
<item>
<date_added>1/1/2014</date_added>
<added_by person="person09" />
</item>
<item>
<date_added>1/9/2014</date_added>
<added_by person="person45" />
</item>
<numitems_current>7</numitems_current>
<manager person="person38" />
</item_list>
I've tried doing something similar to:
我试过做类似的事情:
NodeList nodes = queryDoc.getElementsByTagName("item_list");
for(int i = 0; i < nodes.getLength(); i++) {
Node node = nodes.item(i);
if(node != null) {
System.out.println(node.manager);
}
}
And messing around with this code for a while, but I would like to know how to retrieve data from various fields in each node.
并弄乱了这段代码一段时间,但我想知道如何从每个节点的各个字段中检索数据。
采纳答案by Kartic
If you are trying to read person attribute of manager tag, you can do it as shown below -
如果您正在尝试读取经理标签的人员属性,您可以按如下所示进行 -
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.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class Test{
public static void main (String[] args)
{
Test test = new Test();
test.readXML();
}
private void readXML()
{
Document doc = null;
try
{
doc = parseXML("/home/abc/Test.xml");
}
catch (ParserConfigurationException e)
{
e.printStackTrace();
}
catch (SAXException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
if(doc != null)
{
NodeList nList = doc.getElementsByTagName("item_list");
for (int i = 0; i < nList.getLength(); i++)
{
Node nNode = nList.item(i);
Element eElement = (Element) nNode;
Element cElement = (Element) eElement.getElementsByTagName("manager").item(0);
System.out.println("Manager ID : " + cElement.getAttribute("person"));
}
}
}
private Document parseXML(String filePath) throws ParserConfigurationException, SAXException, IOException
{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(filePath);
doc.getDocumentElement().normalize();
return doc;
}
}
回答by AdrianJR
Or, using xml you might need to edit the initial content. I suggest the following approach
或者,使用 xml 您可能需要编辑初始内容。我建议以下方法
import java.io.IOException;
import java.io.StringWriter;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.*;
import org.xml.sax.SAXException;
public class ReadXML {
public static void main(String[] args) {
try {
Document doc = getDocument("/home/abc/Test.xml");
System.out.println(getString(getNodeByName(doc,"item_list01")));
} catch (TransformerException | ParserConfigurationException | IOException | SAXException e) {
// Log e.printStackTrace();
}
}
private static Document getDocument(String filePath) throws ParserConfigurationException, IOException, SAXException {
DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
return docBuilder.parse(filePath);
}
private static String getString(Node node) throws TransformerException {
StringWriter sw = new StringWriter();
Transformer t = TransformerFactory.newInstance().newTransformer();
t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
t.setOutputProperty(OutputKeys.INDENT, "yes");
t.transform(new DOMSource(node), new StreamResult(sw));
return sw.toString();
}
public static Node getNodeByName(Document doc, String nodeName) {
Node node = null;
for (int i = 0; i < doc.getDocumentElement().getChildNodes().getLength(); i++) {
if (!getTagName(doc, i).equals("#text")) {
for (int j = 0; j < getNodeName(doc, i).getChildNodes().getLength(); j++) {
if (getNodeName(doc, i, j).equalsIgnoreCase("item_list") && getNodeAttributes(doc,i,j).equalsIgnoreCase(nodeName)) {
node = getNodeName(doc, i);
}
}
}
}
return node;
}
private static String getTagName(Document doc, int i) {
return getNodeName(doc, i).getNodeName();
}
private static Node getNodeName(Document doc, int i) {
return (doc.getDocumentElement().getChildNodes().item(i));
}
private static String getNodeName(Document doc, int i, int j) {
return getNodeName(doc, i).getChildNodes().item(j).getNodeName();
}
private static String getNodeAttributes(Document doc, int i, int j) {
if(getNodeName(doc, i).getChildNodes().item(j).hasAttributes()){
return getNodeName(doc, i).getChildNodes().item(j).getAttributes().item(0).getNodeValue();
}
return "";
}
}