[英]Deserialize xml file on android studio
我編寫了一個使用創建的xml文件的應用程序,需要將其反序列化為列表。 列表中的每個項目都有一些屬性和一個內部列表。 例如: <persons> <person> <FirstName>fn1</FirstName> <LastName>ln1</LastName> <Age>30</Age> <FavoriteColors> <ColorItem> <ColorName>red</ColorName> <IsFavorite>True</IsFavorite> </ColorItem> <ColorItem> <ColorName>blue</ColorName> <IsFavorite>False</IsFavorite> </ColorItem> </FavoriteColors> </person> <person> <FirstName>fn2</FirstName> <LastName>ln2</LastName> <Age>20</Age> <FavoriteColors> <ColorItem> <ColorName>white</ColorName> <IsFavorite>False</IsFavorite> </ColorItem> <ColorItem> <ColorName>black</ColorName> <IsFavorite>False</IsFavorite> </ColorItem> <ColorItem> <ColorName>pink</ColorName> <IsFavorite>True</IsFavorite> </ColorItem> </FavoriteColors> </person> </persons>
我知道如何在c#上進行操作,但是我是java的新手,所以找不到解決方法。 最好,更短和最簡單的方法是什么? 我需要像在c#中那樣為其構建類嗎? 還是有一些命令可以遍歷xml元素來建立我的列表? json比xml更好嗎? 如果是這樣,我如何反序列化json? 感謝您的幫手! :)
對於那些需要解決方案的人,這就是我解決的方法。 我使用了dom解析器。
首先,我為最喜歡的顏色及其屬性創建了一個類。
public class FavoriteColors
{
private String ColorName;
private boolean IsFavorite;
public FavoriteColors(String _colorName, boolean _isFavorite)
{
this.ColorName = _colorName;
this.IsFavorite = _isFavorite;
}
}
然后,我為具有該屬性的人創建了一個類。
public class Person
{
private String FirstName;
private String LastName;
private String Age;
private List<FavoriteColors> AllColors = new ArrayList<FavoriteColors>();
public Person(String _firstName, String _lastName, String _age, List<FavoriteColors> _allColors)
{
this.FirstName = _firstName;
this.LastName = _lastName;
this.Age = _age;
for (int i = 0; i < _allColors.size(); i++)
{
this.AllColors.add(_allColors.get(i));
}
}
}
解析代碼:您需要將xml文件讀取為一個字符串(或以您選擇的任何其他方式),並且所有數據收集到的最終列表是listOfPersons,我在代碼中定義了該列表。
public static void main(String[] args)
{
String xmlFile = "your xml file" // read your xml file to string
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try
{
DocumentBuilder builder = factory.newDocumentBuilder();
InputSource is = new InputSource(new StringReader(xmlFile));
Document document = builder.parse(is);
List<Person> listOfPersons = new ArrayList<Person>();
NodeList nodeList = document.getDocumentElement().getChildNodes();
for (int i = 0; i < nodeList.getLength(); i++)
{
Node node = nodeList.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE)
{
Element elem = (Element) node;
String firstNameText = "";
if (elem.getElementsByTagName("FirstName").item(0).getChildNodes().item(0) != null)
{
firstNameText = elem.getElementsByTagName("FirstName").item(0).getChildNodes().item(0).getNodeValue();
}
String lastNameText = "";
if (elem.getElementsByTagName("LastName").item(0).getChildNodes().item(0) != null)
{
lastNameText = elem.getElementsByTagName("LastName").item(0).getChildNodes().item(0).getNodeValue();
}
String ageText = "";
if (elem.getElementsByTagName("Age").item(0).getChildNodes().item(0) != null)
{
ageText = elem.getElementsByTagName("Age").item(0).getChildNodes().item(0).getTextContent();
}
List<FavoriteColors> listOfColors = new ArrayList<FavoriteColors>();
NodeList nl = elem.getElementsByTagName("FavoriteColors").item(0).getChildNodes();
for (int j = 0; j < nl.getLength(); j++)
{
Node n = nl.item(j);
if (n.getNodeType() == Node.ELEMENT_NODE)
{
Element e = (Element) n;
String colorName = "";
if (e.getElementsByTagName("ColorName").item(0).getChildNodes().item(0) != null)
{
colorName = e.getElementsByTagName("ColorName").item(0).getChildNodes().item(0).getNodeValue();
}
boolean isFavorite = false;
if (e.getElementsByTagName("IsFavorite").item(0).getChildNodes().item(0).getNodeValue() != null)
{
isFavorite = Boolean.parseBoolean(e.getElementsByTagName("IsFavorite").item(0).getChildNodes().item(0).getNodeValue());
}
listOfColors.add(new FavoriteColors(colorName, isFavorite));
}
}
listOfPersons.add(new Person(firstNameText, lastNameText, ageText, listOfColors));
}
}
}
catch (ParserConfigurationException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
希望它能幫助別人。 :)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.