Показать сообщение отдельно
Непрочитано 19.09.2016, 20:39   #2

По умолчанию Re: Замена nodeValue по nodeNameItem

PHP код:
public static void main(String[] argsthrows IOExceptionParserConfigurationExceptionTransformerException {
        
DocumentBuilderFactory factory DocumentBuilderFactory.newInstance();
        
Document document;
        try {
            
document factory.newDocumentBuilder().parse(new File("test.xml"));
            final 
NodeList nodeList document.getFirstChild().getChildNodes();
            
IntStream.range(0nodeList.getLength()).forEach(-> {
                
Node node nodeList.item(i);
                
IntStream.range(0node.getChildNodes().getLength()).forEach(-> {
                    final 
Node nodeBlock node.getChildNodes().item(a);
                    if(
nodeBlock.hasAttributes()) {
                        
IntStream.range(0nodeBlock.getAttributes().getLength()).forEach(indexAtt -> {
                            
Node attribute nodeBlock.getAttributes().item(indexAtt);
                            if(
attribute.getNodeName().equals("name")) {
                                
attribute.setNodeValue("finfan " ThreadLocalRandom.current().nextInt());
                            }
                            else {
                                
attribute.setNodeValue(String.valueOf(ThreadLocalRandom.current().nextInt()));
                            }
                        });
                    }
                });
            });
            
TransformerFactory.newInstance().newTransformer().transform(new DOMSource(document), new StreamResult(new File("new_test.xml")));
        }
        catch(
SAXException e) {
            
e.printStackTrace();
        }
    } 
old xml ...
PHP код:
<list>
    <
test name="finfan">
        <
set name="finfan" value="replace"/>
    </
test>
    <
test name="finfan">
        <
set name="finfan" value="replace"/>
    </
test>
    <
test name="finfan">
        <
set name="finfan" value="replace"/>
    </
test>
</list> 

new replace all xml =\


PHP код:
<?xml version="1.0" encoding="UTF-8" standalone="no"?/>
<list>
    <
test name="finfan">
        <
set name="finfan -1501716197" value="-977792414"/>
    </
test>
    <
test name="finfan">
        <
set name="finfan -392286072" value="-230381045"/>
    </
test>
    <
test name="finfan">
        <
set name="finfan 1510237655" value="1910193530"/>
    </
test>
</list>
__________________
Mangol вне форума Отправить сообщение для Mangol с помощью ICQ Ответить с цитированием
Сказали спасибо: