// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.io.ByteArrayInputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.traversal.DocumentTraversal;
import org.w3c.dom.traversal.NodeFilter;
import org.w3c.dom.traversal.TreeWalker;

public class NSFun {
    public static void dump(String s) throws Exception {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(new ByteArrayInputStream(s.getBytes()));
        TreeWalker walk = ((DocumentTraversal)doc).createTreeWalker(doc, NodeFilter.SHOW_ELEMENT, null, false);
        Node n;
        while((n = walk.nextNode()) != null) {
            System.out.println(n.getNamespaceURI() + " " + n.getLocalName());
        }
    }
    public static void main(String[] args) throws Exception {
        dump("<?xml version='1.0' encoding='UTF-8' standalone='yes'?><MSG xmlns='http://xxx.yyy.com/'><ACKNOWLEDGEMENT>Success</ACKNOWLEDGEMENT></MSG>");
        dump("<?xml version='1.0' encoding='UTF-8' standalone='yes'?><MSG xmlns:ns2='http://xxx.yyy.com/'><ns2:ACKNOWLEDGEMENT>Success</ns2:ACKNOWLEDGEMENT></MSG>");
    }
}
