I'm having a bit of a problem trying to loop through all the nodes in an XML string then updating the values. Please be aware I'm still fairly new with Java.
My aim is to go through every Element and Attribute then run a RegEx on each value to make sure the fields contain only a predefined set of characters. If the field contains unwanted characters then these will be removed and the field updated.
I'm probably doing this completly wrong but the problem comes in when trying to edit the children of children, please see my code below.
protected NodeList checkXML(Node node, String strStripCharsRegEx) {
String strNodeResult = "";
//NodeList nodeResult = null;
// do something with the current node instead of System.out
System.out.println(node.getNodeName());
strNodeResult = "";
if(node.getNodeValue() != null && node.getNodeValue() != "")
{
for(char c : node.getNodeValue().toCharArray()) {
if(Character.toString(c).matches(strStripCharsRegEx))
strNodeResult = strNodeResult + c;
}
if(strNodeResult != "")
{
node.setNodeValue(strNodeResult);
}
}
if(node.hasAttributes())
{
NamedNodeMap XMLAttributes = node.getAttributes();
if(XMLAttributes != null)
{
for(int attribIndex=0; attribIndex< XMLAttributes.getLength(); attribIndex++)
{
System.out.println("AttribName = " + XMLAttributes.item(attribIndex).getNodeName());
if(XMLAttributes.item(attribIndex).getNodeValue() != null)
{
if(XMLAttributes.item(attribIndex).getNodeValue() != null && XMLAttributes.item(attribIndex).getNodeValue() != "")
{
strNodeResult = "";
for(char c : XMLAttributes.item(attribIndex).getNodeValue().toCharArray())
{
if(Character.toString(c).matches(strStripCharsRegEx))
strNodeResult = strNodeResult + c;
}
if(strNodeResult != "")
{
XMLAttributes.item(attribIndex).setNodeValue(strNodeResult);
}
}
System.out.println("AttribValue = " + XMLAttributes.item(attribIndex).getNodeValue());
}
}
}
}
//Check for Children
NodeList nodeList = node.getChildNodes();
if(nodeList != null && node.hasChildNodes())
{
for (int i = 0; i < nodeList.getLength(); i++) {
Node currentNode = nodeList.item(i);
if (currentNode.getNodeType() == Node.ELEMENT_NODE) {
if(currentNode.hasChildNodes())
{
//calls this method for all the children which is Element
checkXML(currentNode, strStripCharsRegEx);
}
}
}
}
return nodeList;
}
Any help would be appreciated.
Thanks
Andy