Perl code for Find and replace a tag value in XML

后端 未结 3 434

Below is the XML I will be using:

ABC

3条回答
  •  感情败类
    2021-01-14 16:02

    XPath is able to find nodes without needing to know the position in the tree.

    use strictures;
    use XML::LibXML qw();
    my $dom = XML::LibXML->load_xml(string => <<'XML');
    
    ABC
    
    
    
    XML
    
    for my $id ($dom->findnodes('//id[string()="ABC"]')) {
        $id->removeChildNodes;
        $id->appendText('DEF');
    }
    
    print $dom->toString
    

提交回复
热议问题