XML 到哈希表

发布于 2024-11-06 15:24:49 字数 1489 浏览 0 评论 0原文

目前我有下面的代码将数据放入哈希中。 我的问题:我必须在 !!!SOMETHING!!! 部分放入哪个值。 该代码只需读取一个元素标签并将其值插入哈希表中。

public void ReadXML(){
    try {

        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.parse(fileout);
        doc.getDocumentElement().normalize();
        Hashtable hash = new Hashtable();

        NodeList dataNodes = doc.getElementsByTagName("DataArea");
//      getChildNodes().item(0).getChildNodes();
        Element root = doc.getDocumentElement();
        String dataNodeIndex = root.toString();
        System.out.println("");

    for (int dataNodeIndex1 = 0; dataNodeIndex1 < dataNodes.getLength(); dataNodeIndex1++)
        {
            Node nodeName = dataNodes.item(dataNodeIndex1);

            if (nodeName.getNodeType() == Node.ELEMENT_NODE) {
                Element elementName = (Element) nodeName;
                NodeList elementNameList = elementName.getElementsByTagName(elementtag1);
                Element elementName2 = (Element) elementNameList.item(0);
                NodeList nameElement = elementName2.getChildNodes();
                System.out.println("NodeContent: "  + ((Node) nameElement.item(0)).getNodeValue());

            }
            hash.put(elementtag1, !!!SOMETHING!!!);

            System.out.println(hash);
        }       


    }



    catch(Exception e){ 
        e.printStackTrace();
    }


}

Currently I have the code below to put the data in a Hash.
My question: which value do i have to put in the part of !!!SOMETHING!!!.
The code only has to read one elementtag and insert it's value in the hashtable.

public void ReadXML(){
    try {

        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.parse(fileout);
        doc.getDocumentElement().normalize();
        Hashtable hash = new Hashtable();

        NodeList dataNodes = doc.getElementsByTagName("DataArea");
//      getChildNodes().item(0).getChildNodes();
        Element root = doc.getDocumentElement();
        String dataNodeIndex = root.toString();
        System.out.println("");

    for (int dataNodeIndex1 = 0; dataNodeIndex1 < dataNodes.getLength(); dataNodeIndex1++)
        {
            Node nodeName = dataNodes.item(dataNodeIndex1);

            if (nodeName.getNodeType() == Node.ELEMENT_NODE) {
                Element elementName = (Element) nodeName;
                NodeList elementNameList = elementName.getElementsByTagName(elementtag1);
                Element elementName2 = (Element) elementNameList.item(0);
                NodeList nameElement = elementName2.getChildNodes();
                System.out.println("NodeContent: "  + ((Node) nameElement.item(0)).getNodeValue());

            }
            hash.put(elementtag1, !!!SOMETHING!!!);

            System.out.println(hash);
        }       


    }



    catch(Exception e){ 
        e.printStackTrace();
    }


}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

多孤肩上扛 2024-11-13 15:24:49

您应该使用我找到的这些方法:

protected String getString(String tagName, Element element) {
    NodeList list = element.getElementsByTagName(tagName);
    if (list != null && list.getLength() > 0) {
        NodeList subList = list.item(0).getChildNodes();

        if (subList != null && subList.getLength() > 0) {
            return subList.item(0).getNodeValue();
        }
    }

    return null;
}

像这样使用它:

if (NodeName.getNodeType() == Node.ELEMENT_NODE) {
    Element ElementName = (Element) NodeName;
    Hash.put(Elementtag1, getString(Elementtag1, ElementName));

}

检查一下:

http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/

如何使用 Java 检索 XML 的元素值?

You should use these method that i found :

protected String getString(String tagName, Element element) {
    NodeList list = element.getElementsByTagName(tagName);
    if (list != null && list.getLength() > 0) {
        NodeList subList = list.item(0).getChildNodes();

        if (subList != null && subList.getLength() > 0) {
            return subList.item(0).getNodeValue();
        }
    }

    return null;
}

use it like this :

if (NodeName.getNodeType() == Node.ELEMENT_NODE) {
    Element ElementName = (Element) NodeName;
    Hash.put(Elementtag1, getString(Elementtag1, ElementName));

}

Check it out :

http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/

and

How to retrieve element value of XML using Java?

心病无药医 2024-11-13 15:24:49

您为此操作选择了不正确的集合类型,如果您想将元素标记值保存在 Set 中,是的,最好使用 HashSet 但要实现 HashSet 大约是您尝试做的,因此 Set 的值像键一样放入 HashMap 中,但您可以使用另一个集合,例如 List队列堆栈尝试找到更好的 为你。
也许 SAX 对您来说会更好 DOM ...

You have chosen incorrect collection type for this operation, if you wanna save your element tag values in Set yes it is better to use HashSet but implementation of HashSet approximately you try to do, so values of Set puts into HashMap like keys, but you can use another collection like List, Queue, Stack try to find better for you.
And maybe SAX will be better DOM for you ...

去了角落 2024-11-13 15:24:49

为了使事情变得更简单、更强大,您可以使用 Properties 相反,它具有 Hashtable(它实际上扩展了它),并且可以导入和导出 XML(请参阅 loadFromXMLstoreToXML 方法)。请参阅 http://www.ibm.com/developerworks/java/详细信息请参见library/j-tiger02254/index.html

To make things easier and more robust, you could use a Properties instead, which has an underlying implementation of a Hashtable (it actually extends it) and can import and export to/from XML (see loadFromXML and storeToXML methods). See http://www.ibm.com/developerworks/java/library/j-tiger02254/index.html for details.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文