問題描述
我是 Java 和 XML DOM 解析器的新手.我有一個(gè)要求,比如讀取 xml 數(shù)據(jù)并將其存儲(chǔ)為列和行類型的通知.示例:sample.xml 文件
I am new working in Java and XML DOM parser. I had a requirement like read the xml data and store it inform of column and rows type. Example:sample.xml file
<staff>
<firstname>Swetha</firstname>
<lastname>EUnis</lastname>
<nickname>Swetha</nickname>
<salary>10000</salary>
</staff>
<staff>
<firstname>John</firstname>
<lastname>MAdiv</lastname>
<nickname>Jo</nickname>
<salary>200000</salary>
</staff>
我需要讀取這個(gè) XML 文件并以上述格式存儲(chǔ):
i need to read this XML file and store it in the above format:
firstName,lastName,nickName,Salary
swetha,Eunis,swetha,10000
john,MAdiv,Jo,200000
Java 代碼:
NodeList nl= doc.getElementsByTagName("*");
for(int i=0;i< nl.getLength();i++)
{
Element section = (Element) nl.item(i);
Node title = section.getFirstChild();
while (title != null && title.getNodeType() != Node.ELEMENT_NODE)
{
title = title.getNextSibling();
if (title != null)
{
String first=title.getFirstChild().getNodeValue().trim();
if(first!=null)
{
title = title.getNextSibling();
}
System.out.print(first + ",");
} }
System.out.println("");
}//for
我做了上面的代碼,但我無法找到以上述列和行格式獲取數(shù)據(jù)的方法.任何人都可以請(qǐng)幫我解決我的問題,我從過去的很多天里都在調(diào)查它
I did the above code, but i am not able to find the way to get the data in the above column and row format. Can any one please please kindly help me in solving my issue, i am looking into it from past many days
推薦答案
由于這看起來像家庭作業(yè),所以我會(huì)給你一些提示:
Since this looks like homework, I'm going to give you some hints:
很可能你的講師給了你一些關(guān)于處理 XML DOM 的講義和/或示例.再讀一遍.
The chances are that your lecturer has given you some lecture notes and/or examples on processing an XML DOM. Read them all again.
getElementsByTagName
方法將元素名稱作為參數(shù)."*"
不是有效的元素名稱,因此調(diào)用不會(huì)返回任何內(nèi)容.
The getElementsByTagName
method takes an element name as a parameter. "*"
is not a valid element name, so the call won't return anything.
您的代碼需要反映 XML 的結(jié)構(gòu).本例中的 XML 結(jié)構(gòu)由 N 個(gè) staff
元素組成,每個(gè)元素都包含名為 firstname
、lastname
、nickname
和 salary
.
Your code needs to mirror the structure of the XML. The XML structure in this case consists of N staff
elements, each of which contains elements named firstname
, lastname
, nickname
and salary
.
您的講師也可能希望您使用類似 XSLT 或 XML 綁定機(jī)制的東西來簡化這一點(diǎn).(或者也許這個(gè) 是 旨在成為 XMI 而不是 XML ......其中還有其他方法來處理這個(gè)......)
It is also possible that your lecturer expects you to use something like XSLT or an XML binding mechanism to simplify this. (Or maybe this was intended to be XMI rather than XML ... in which there are other ways to handle this ...)
我保留getElementsByTagName方法參數(shù)*",因?yàn)橐獎(jiǎng)討B(tài)讀取數(shù)據(jù).
I kept getElementsByTagName method parameter "*" because to read the data dynamically.
好吧,它不起作用!!DOM getElementsByTagName
方法不接受任何類型的模式.
Well, it doesn't work!! The DOM getElementsByTagName
method does NOT accept a pattern of any kind.
如果你想讓你的代碼通用,你不能使用getElementsByTagName
.您需要從頂部開始遍歷樹,從 DOM 的根節(jié)點(diǎn)開始.
If you want to make your code generic, you can't use getElementsByTagName
. You will need to walk the tree from the top, starting with the DOM's root node.
能否請(qǐng)您提供示例數(shù)據(jù).
Can you please provide me with sample data.
沒有.你的講師不同意我給你復(fù)制代碼.但是,我要指出,網(wǎng)絡(luò)上有很多 XML DOM 教程,它們應(yīng)該可以幫助您弄清楚您需要做什么.最好的事情是你自己做這項(xiàng)工作.這樣你會(huì)學(xué)到更多……這就是你作業(yè)的重點(diǎn)!
No. Your lecturer would not approve of me giving you code to copy from. However, I will point out that there are lots of XML DOM tutorials on the web which should help you figure out what you need to do. The best thing is for you to do the work yourself. You will learn more that way ... and that is the whole point of your homework!
這篇關(guān)于如何在 Dom 解析器中修改 XML 數(shù)據(jù)的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!