1

XML 文字列内のすべてのノードをループして値を更新しようとすると、少し問題が発生します。私はまだ Java にかなり慣れていないことに注意してください。

私の目的は、すべての要素と属性を調べてから、各値に対して正規表現を実行して、フィールドに事前定義された文字セットのみが含まれていることを確認することです。フィールドに不要な文字が含まれている場合、これらは削除され、フィールドが更新されます。

私はおそらくこれを完全に間違っていますが、子供の子供を編集しようとすると問題が発生します。以下のコードを参照してください。

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;
}

どんな助けでも大歓迎です。

ありがとう

アンディ

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 に答える