0

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


}
Eve
  • 514
  • 3
  • 12
  • 23
  • You could use `null`, though you're probably better off using a `HashSet` if your table isn't storing key-value pairs. [EDIT] Sorry, `null` won't work as a value for `Hashtable`. Use any object that you don't care about, that won't be a serious memory leak. – Nathan Ryan May 13 '11 at 12:32
  • So what if i want to copy it all, I could break it apart myself. But i just need one good example to know what to do. i saw an example with accountinformation and transactions, but it didn't help me because i'm not adding anything up. – Eve May 13 '11 at 12:58
  • this code is messed up. what is `ElementTag1`? In java variables NEVER start with uppercase letters. your mistake is now fooling stackoverflow's syntax highlighter. – ahmet alp balkan May 13 '11 at 13:01
  • ElementTag1 is a global variable i put in, in the code it still accepts the tag. It refers to a xmltag name i defined. – Eve May 13 '11 at 13:09

3 Answers3

1

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?

Community
  • 1
  • 1
EricParis16
  • 809
  • 5
  • 9
0

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 ...

jitm
  • 2,569
  • 10
  • 40
  • 55
0

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.

Chris Dennett
  • 22,412
  • 8
  • 58
  • 84