![](/img/trans.png)
[英]Using java, how would I read an xml file from a website then put the data from the xml file in a JList?
[英]Read data from XML file (in Java) using Reflection
給定一個簡單的XML文件,例如:
<Game>
<Round>
<roundNumber>1</roundNumber>
<Door>
<doorName>abd11</doorName>
<Value>
<xVal1>0</xVal1>
<xVal2>25</xVal2>
<pVal>0.31</pVal>
</Value>
<Value>
<xVal1>25</xVal1>
<xVal2>50</xVal2>
<pVal>0.04</pVal>
</Value>
<Value>
<xVal1>50</xVal1>
<xVal2>75</xVal2>
<pVal>0.19</pVal>
</Value>
<Value>
<xVal1>75</xVal1>
<xVal2>100</xVal2>
<pVal>0.46</pVal>
</Value>
</Door>
<Door>
<doorName>vvv1133</doorName>
<Value>
<xVal1>60</xVal1>
<xVal2>62</xVal2>
<pVal>1.0</pVal>
</Value>
</Door>
</Round>
<Round>
<roundNumber>2</roundNumber>
<Door>
<doorName>eee</doorName>
<Value>
<xVal1>0</xVal1>
<xVal2>-25</xVal2>
<pVal>0.31</pVal>
</Value>
<Value>
<xVal1>-25</xVal1>
<xVal2>-50</xVal2>
<pVal>0.04</pVal>
</Value>
<Value>
<xVal1>-50</xVal1>
<xVal2>-75</xVal2>
<pVal>0.19</pVal>
</Value>
<Value>
<xVal1>-75</xVal1>
<xVal2>-100</xVal2>
<pVal>0.46</pVal>
</Value>
</Door>
<Door>
<doorName>cc</doorName>
<Value>
<xVal1>-60</xVal1>
<xVal2>-62</xVal2>
<pVal>0.3</pVal>
</Value>
<Value>
<xVal1>-70</xVal1>
<xVal2>-78</xVal2>
<pVal>0.7</pVal>
</Value>
</Door>
</Round>
</Game>
我想從該文件中讀取數據。
我可以按照讀取標簽的老式方式進行操作,並相應地創建對象,
但是我想使用反射機制做到這一點。
有人可以解釋或指導我學習可以解釋該操作方法的教程嗎?
謝謝
編輯:
我做了以下事情:
import java.io.File;
import org.w3c.dom.Document;
import org.w3c.dom.*;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
public class ReadAndPrintXMLFile {
public static void main (String argv []){
try {
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.parse (new File("input.xml"));
// ROOT
// normalize text representation
doc.getDocumentElement ().normalize ();
System.out.println ("Root element of the doc is " +
doc.getDocumentElement().getNodeName());
// Number of nodes/rounds
NodeList listOfItems = doc.getElementsByTagName("Round");
int totalElements = listOfItems.getLength();
System.out.println("Total number of nodes : " + totalElements );
...
...
}
這是部分代碼,我運行了代碼並對其進行了測試,此刻,使用SAX,我可以從上面的XML文件讀取內容。
現在,我要做的就是使用Reflection處理數據。 根據我的理解,反射僅適用於現有對象。 因此,是否足以將數據(即回合)存儲在變量listOfItems
然后在其上使用Reflection?
再次感謝 !
好吧,在這種情況下,我想您可以按照paulsm4的建議使用Java XML api將XML文件讀入Java,並使用反射來獲取和處理它們。
http://java.sun.com/developer/technicalArticles/ALT/Reflection/
這已經死了,所以試試這個。 http://docs.oracle.com/javase/tutorial/reflect/index.html
這不是最好的教程,但足以滿足您的需求。 並且我可能建議這是反射API的一種非常差的用法,因此我想向任何給您家庭作業的人扔雞蛋:-D
編輯:是的,如果您希望可以在其上使用反射。或者您可以創建一個對象模型(門,圓形和游戲類)並在XML文件中填充它們的值,然后對任何需要使用的對象進行反射完成。 由您決定如何最好地做到這一點!
您為什么不閱讀使用Java XML API之一的“老式方法”?
Google用於“ Java XML”,“ SAX”,“ DOM”,“ DocumentBuilder”,“ org.w3c.dom”等,您會發現很多不錯的文檔和教程。 例如:
你有類似的東西:
GameRound >>
int roundNumber
Door door >>
String doorName
Value value >>
int xVal1
int xVal2
float pVal
您希望解析XML並填充對象嗎? 這個對嗎?
如果是的話,我看不出重點! 即使您不想使用任何api,也可以編寫自己的API並通過setter填充值!
@Thihara +1雞蛋xD
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.