public static Node addDomAttr ( Document doc , String tagName , String tagNamespaceURI , String attrName , String attrValue ) { // Create a Document to work on Document newDoc = null ; try { System . setProperty ( "javax.xml.parsers.DocumentBuilderFactory" , "org.apache.xerces.jaxp.DocumentBuilderFactoryImpl" ) ; DocumentBuilderFactory dbf = DocumentBuilderFactory . newInstance ( ) ; dbf . setNamespaceAware ( true ) ; DocumentBuilder db = dbf . newDocumentBuilder ( ) ; newDoc = db . newDocument ( ) ; } catch ( Exception e ) { e . printStackTrace ( ) ; // Fortify Mod: If we got here there is no point going any further return null ; } Transformer identity = null ; try { TransformerFactory TF = TransformerFactory . newInstance ( ) ; // Fortify Mod: disable external entity injection TF . setFeature ( XMLConstants . FEATURE_SECURE_PROCESSING , true ) ; identity = TF . newTransformer ( ) ; // End Fortify Mod identity . transform ( new DOMSource ( doc ) , new DOMResult ( newDoc ) ) ; } catch ( Exception ex ) { System . out . println ( "ERROR: " + ex . getMessage ( ) ) ; } // Get all named nodes in the doucment NodeList namedTags = newDoc . getElementsByTagNameNS ( tagNamespaceURI , tagName ) ; for ( int i = 0 ; i < namedTags . getLength ( ) ; i ++ ) { // Add the attribute to each one Element element = ( Element ) namedTags . item ( i ) ; element . setAttribute ( attrName , attrValue ) ; } // displayNode(newDoc); return ( Node ) newDoc ; }
Adds the attribute to each node in the Document with the given name .