将XML文件读入hashmap并创建新对象



我试图将XML读取到HashMap,我试图读取每个XML节点并根据其内容创建新对象。

如何检索XML的每个节点的值?

例如:我需要在我的For循环中检索birthdate值,并使用它来创建一个新的studentInfo对象。

xml:

<students xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:noNamespaceSchemaLocation="students.xsd"><description>A bunch students and courses</description><student studentID="0144085" gender ="M"><firstname>Jack</firstname>
  <lastname>Blogs</lastname> <birthday day="21" month="04" year="1983"/><paper>Data Structures and Algorithms</paper><paper>Distributed and Mobile Systems</paper>     <paper>Software Engineering</paper><paper>Highly Secure Systems</paper><paper>Engineering Computations</paper><paper>Object Oriented Programming</paper></student>

code:

                //jf : set StudentINfoSet class properties
            this.description =  rootXMLNode.getElementsByTagName( "description" ).item( 0 ).getTextContent();
            studentMap =  new HashMap<String, StudentInfo>();
            NodeList nodeList = document.getElementsByTagName( "student" );
            for (int i = 0; i < nodeList.getLength(); i++) {
                Node currentNode = nodeList.item(i);
                StudentInfo si = new StudentInfo(Integer.toString(i)){};
                /*
                 * String studentID, String firstName, String lastName,
        String birthdate, String gender, char studentGender
                 * */
                this.studentMap.put(Integer.toString(i), si);
            }
            System.out.println("Number of students : "+nodeList.getLength());

我将使用JAXB,参见这里的示例http://www.mkyong.com/java/jaxb-hello-world-example/

您应该考虑jaxb或简单框架来实现这一点,而不是编写for循环并遍历XML节点

应该使用对象的唯一标识符作为hashmap键。我相信在您的情况下,studentID将是您的哈希映射键的一个很好的候选。相应地定义hashmap并根据建议保存元素。您应该能够使用studententid轻松地从hashmap中检索每个学生。

希望有帮助!

我想我已经解决了这个问题。这是我的代码。谢谢你的帮助。
public StudentInfoSet(InputStream in) {
    if (in != null) {
        try {
            DocumentBuilderFactory builderFactory = DocumentBuilderFactory
                    .newInstance();
            builderFactory.setNamespaceAware(true);
            builderFactory.setValidating(false);
            builderFactory.setAttribute(JAXP_SCHEMA_LANGUAGE,W3C_XML_SCHEMA);
            DocumentBuilder builder = builderFactory.newDocumentBuilder();
            // parse the input stream
            document = builder.parse(in);
            document.getDocumentElement().normalize();
            // JAXP
            Element rootXMLNode = document.getDocumentElement();
            //jf : set StudentINfoSet class properties
            this.description =  rootXMLNode.getElementsByTagName( "description" ).item( 0 ).getTextContent();
            studentMap =  new HashMap<String, StudentInfo>();
            NodeList nodeList = document.getElementsByTagName( "student" );
            for (int i = 0; i < nodeList.getLength(); i++) {
                Node currentNode = nodeList.item(i);
                StudentInfo si = new StudentInfo(Integer.toString(i)){};
                /*
                 * String studentID, String firstName, String lastName,
        String birthdate, String gender, char studentGender
                 * */
                //this.studentMap.put(Integer.toString(i), si);
                System.out.println("nCurrent Element :" + currentNode.getNodeName());
                if (currentNode.getNodeType() == Node.ELEMENT_NODE) {
                    Element eElement = (Element) currentNode;
                    System.out.println("Staff id : " + eElement.getAttribute("firstname"));
                    System.out.println("First Name : " + eElement.getElementsByTagName("lastname").item(0).getTextContent());
                    System.out.println("Last Name : " + eElement.getElementsByTagName("birthday").item(0).getTextContent());

                    NodeList studentPaperNodeList = eElement.getElementsByTagName("paper");
                    for (int j = 0; j < studentPaperNodeList.getLength(); j++) {
                        Element paperNodeElement = (Element)studentPaperNodeList.item(j);
                        System.out.println("nCurrent paper :" + paperNodeElement.getTextContent());
                    }
                }
            }
            System.out.println("Number of students : "+nodeList.getLength());

        } catch (FactoryConfigurationError e) {
            e.printStackTrace();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

相关内容

  • 没有找到相关文章

最新更新