1

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

4

2 回答 2

1

首先你不需要自己解析XML,很多XML解析器都可以解析XML,你可以在解析后编辑值,然后再将它们转换为XML。您可以为此使用 dom4j。

http://dom4j.sourceforge.net/

于 2012-04-16T10:42:53.307 回答
0

你真的想用 Java 编写这种代码吗?在专为这项工作而设计的 XSLT 中,这要容易得多。您可以轻松地从 Java 调用 XSLT。

于 2012-04-16T12:02:55.790 回答