【问题标题】:Deserialize xml file on android studio在android studio上反序列化xml文件
【发布时间】:2018-07-28 23:36:28
【问题描述】:

我编写了一个使用我创建的 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?感谢帮助者! :)

【问题讨论】:

    标签: java xml android-studio serialization deserialization


    【解决方案1】:

    对于那些需要解决方案的人,这就是我解决它的方法。我使用了dom解析器。

    首先,我用它的属性为 favoriteColors 创建了一个类。

    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();
            }
        }
    

    希望它能帮助其他人。 :)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-10-08
      • 1970-01-01
      • 1970-01-01
      • 2019-09-24
      • 2020-08-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多