Search in sources :

Example 1 with NamespaceAwareHashMap

use of groovy.util.slurpersupport.NamespaceAwareHashMap in project groovy by apache.

the class XmlSlurper method startElement.

/* (non-Javadoc)
    * @see org.xml.sax.ContentHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
    */
public void startElement(final String namespaceURI, final String localName, final String qName, final Attributes atts) throws SAXException {
    addCdata();
    final Map<String, String> attributes = new NamespaceAwareHashMap();
    final Map<String, String> attributeNamespaces = new HashMap<String, String>();
    for (int i = atts.getLength() - 1; i != -1; i--) {
        if (atts.getURI(i).length() == 0) {
            attributes.put(atts.getQName(i), atts.getValue(i));
        } else {
            String key = new QName(atts.getURI(i), atts.getLocalName(i)).toString();
            attributes.put(key, atts.getValue(i));
            attributeNamespaces.put(key, atts.getURI(i));
        }
    }
    final Node newElement;
    if (namespaceURI.length() == 0) {
        newElement = new Node(currentNode, qName, attributes, attributeNamespaces, namespaceURI);
    } else {
        newElement = new Node(currentNode, localName, attributes, attributeNamespaces, namespaceURI);
    }
    if (currentNode != null) {
        currentNode.addChild(newElement);
    }
    stack.push(currentNode);
    currentNode = newElement;
}
Also used : NamespaceAwareHashMap(groovy.util.slurpersupport.NamespaceAwareHashMap) NamespaceAwareHashMap(groovy.util.slurpersupport.NamespaceAwareHashMap) HashMap(java.util.HashMap) QName(groovy.xml.QName) Node(groovy.util.slurpersupport.Node)

Example 2 with NamespaceAwareHashMap

use of groovy.util.slurpersupport.NamespaceAwareHashMap in project groovy-core by groovy.

the class XmlSlurper method startElement.

/* (non-Javadoc)
    * @see org.xml.sax.ContentHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
    */
public void startElement(final String namespaceURI, final String localName, final String qName, final Attributes atts) throws SAXException {
    addCdata();
    final Map<String, String> attributes = new NamespaceAwareHashMap();
    final Map<String, String> attributeNamespaces = new HashMap<String, String>();
    for (int i = atts.getLength() - 1; i != -1; i--) {
        if (atts.getURI(i).length() == 0) {
            attributes.put(atts.getQName(i), atts.getValue(i));
        } else {
            String key = new QName(atts.getURI(i), atts.getLocalName(i)).toString();
            attributes.put(key, atts.getValue(i));
            attributeNamespaces.put(key, atts.getURI(i));
        }
    }
    final Node newElement;
    if (namespaceURI.length() == 0) {
        newElement = new Node(currentNode, qName, attributes, attributeNamespaces, namespaceURI);
    } else {
        newElement = new Node(currentNode, localName, attributes, attributeNamespaces, namespaceURI);
    }
    if (currentNode != null) {
        currentNode.addChild(newElement);
    }
    stack.push(currentNode);
    currentNode = newElement;
}
Also used : NamespaceAwareHashMap(groovy.util.slurpersupport.NamespaceAwareHashMap) NamespaceAwareHashMap(groovy.util.slurpersupport.NamespaceAwareHashMap) HashMap(java.util.HashMap) QName(groovy.xml.QName) Node(groovy.util.slurpersupport.Node)

Aggregations

NamespaceAwareHashMap (groovy.util.slurpersupport.NamespaceAwareHashMap)2 Node (groovy.util.slurpersupport.Node)2 QName (groovy.xml.QName)2 HashMap (java.util.HashMap)2