tags:

views:

16

answers:

0

I have a situation where I want to massage an XML string into an easier format to handle. In a previous project I've found that transforming into

<action>
  <set key="A" value="X" />
  <set key="B" value="Y" />
...
</action>

works very well for our purpose. My problem is with extracting the key-value pairs programatically inside Java after the transformation gave me the above XML in a DOMResult variable. Previously I've worked with

    // DOMResult is now a list of <set key="XXX" value="yyy">. We
    // then create a map of the key, value pairs

    Map<String, String> keyValueMap = new TreeMap<String, String>();

    Node resultRoot = result.getNode().getFirstChild();
    NodeList childNodes = resultRoot.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {

        NamedNodeMap attributes = childNodes.item(i).getAttributes();

        String key = attributes.getNamedItem("key").getTextContent();
        String value = attributes.getNamedItem("value").getTextContent();
        // <x key="a" value="b"/> -> put("a", "b")
        keyValueMap.put(key, value);
    }

and then simply work with the generated keyValueMap. Now the code does not work in the new project I'm porting this too, and the Eclipse debugger does not give me very useful renderings variables of the Node, NodeList and NamedNodeMap types - many "null"-strings and no XML trees. I'd like some useful tips to be able to easier see what is in the values I have in my hand.

Any suggestions?