2

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

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

例如:我需要birthdate在我的 for 循环中检索值并使用它来创建一个新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>

代码 :

                //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());
4

4 回答 4

1

我会使用 JAXB,请参阅此处的示例http://www.mkyong.com/java/jaxb-hello-world-example/

于 2013-05-25T06:39:03.773 回答
1

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

于 2013-05-25T06:44:29.420 回答
0

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

希望能帮助到你!

于 2013-05-25T06:28:14.057 回答
0

我想我已经解决了这个问题。这是我的代码。谢谢您的帮助。

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();
        }
    }
}
于 2013-05-25T07:49:08.500 回答