OpenSSL VPN Serveurs de messagerie |
PERL/XMLParserPERL.XMLParser HistoryShow minor edits - Show changes to markup Changed lines 1-2 from:
Ce code ci-dessous permet la fusion de fichiers XML. to:
Ce code ci-dessous permet la fusion de données au format XML.\\ Changed lines 3-5 from:
L'arborescence d'origine ($xp) est lue jusqu'à ce que la ligne contenant <Domain Name=\"DOIN7L9R\"><Application Name=\"CAC07015108\"> soit détecté. to:
L'objectif de ce code est le parcour d'un arbre principal y détecter un noeud particulier et le mettre à jour ou y insérer un nouvel arbre XML. Changed line 9 from:
- si l'étiquette ajouté contient des fils (getChildNodes != null), ils sont ajoutés (appendChild).\\ to:
- si l'étiquette ajoutée contient des fils (getChildNodes != null), ses fils sont ajoutés (appendChild) au noeud courant.\\ Added lines 1-182:
Ce code ci-dessous permet la fusion de fichiers XML. (:source lang=perl :)
use XML::XPath::XMLParser; use XML::XPath::Node ':node_keys'; $xp = XML::XPath::XMLParser->new(xml=> "<Domain Name=\"DOIN7L9R\"><Application Name=\"CAC07015108\"><EJBComponent Name=\"servicecache-ejb-1.3.0.jar\" /><WebAppComponent Name=\"/CAC01000004/servicecache\" /></Application><Application Name=\"CAC07015100\"><EJBComponent Name=\"servicecache-ejb-1.3.0.jar\" /><WebAppComponent Name=\"/CAC07015100/servicecache\" /></Application></Domain>"); $insert=XML::XPath::XMLParser->new(xml=> "<Application Name=\"CAC07015108\" Name3=\"ff\" Name2=\"ff\"><test/></Application>"); my $root = $xp->parse; $i=0; print "#####avant\n"; Show($root,0); print "\n#####fusion avec\n"; Show($insert->parse,0); print "\n#####fusion ...\n"; Walk($root,""); print "\n#####apres\n"; Show($root,0);
sub Show{ my $root = shift; my $level = shift; foreach $child ($root->getChildNodes){ for (my $a=0;$a<$level;$a++){print " ";} print "<".$child->getName; foreach $attrib ($child->getAttributeNodes){ print $attrib->toString; } print ">\n"; Show($child,$level+1); for ($a=0;$a<$level;$a++){print " ";} print "</".$child->getName.">\n"; } }
sub Walk { my $root = shift; my $XMLParentString = shift; foreach $node ($root->getChildNodes){ if ($node->getNodeType == ELEMENT_NODE()){ $XMLString = $XMLParentString . GetTextNode($node); # test de $XMLString print $XMLString."\n"; # l'événement recherché est trouvé if($XMLString eq "<Domain Name=\"DOIN7L9R\"><Application Name=\"CAC07015108\">"){ $newnodes=($insert)->parse; # L'arborescence XML ne peut avoir qu'une seule racine donc un seul fils $newnode = $newnodes->getChildNode(1); if ($newnode->getName ne $node->getName){ $root->insertAfter($newnodes, $node); } else{ # # Fusion des attributs du node courant avec le nouveau # $isNew = 0; $isMod = 0; foreach $newattrib ($newnode->getAttributes){ $Traitement=0; foreach $attrib ($node->getAttributes){ if($attrib->getName eq $newattrib->getName){ $Traitement = 1; if($attrib->getNodeValue ne $newattrib->getNodeValue){ $attrib->setNodeValue($newattrib->getNodeValue); } } } if( not $Traitement){ $attrib=XML::XPath::Node::Attribute->new($newattrib->getName,$newattrib->getNodeValue); $node->appendAttribute($attrib); } } # # ajout des fils du nouveau node au node courant # foreach $childnode ($newnode->getChildNodes){ $node->appendChild ( $childnode ); } } } Walk($node,$XMLString); } } }
sub GetTextNode { my $node = shift(@_); my $result="<".$node->getName; sub backwards { $a->getName cmp $b->getName; } my @Attributes = sort backwards $node->getAttributes; foreach my $attrib (@Attributes){ $result=$result." ".$attrib->getName."=\"".$attrib->getNodeValue."\""; } $result=$result.">"; return $result; } (:sourcend:) Donnera cette sortie #####avant <Domain Name="DOIN7L9R"> >>red<< <Application Name="CAC07015108">%red% <EJBComponent Name="servicecache-ejb-1.3.0.jar"> </EJBComponent> <WebAppComponent Name="/CAC01000004/servicecache"> </WebAppComponent> </Application> <Application Name="CAC07015100"> <EJBComponent Name="servicecache-ejb-1.3.0.jar"> </EJBComponent> <WebAppComponent Name="/CAC07015100/servicecache"> </WebAppComponent> </Application> </Domain> #####fusion avec <Application Name="toto" Name3="ff" Name2="ff"> <test> </test> </Application> #####fusion ... <Domain Name="DOIN7L9R"> <Domain Name="DOIN7L9R"><Application Name="CAC07015108"> <Domain Name="DOIN7L9R"><Application Name="CAC07015108"><EJBComponent Name="servicecache-ejb-1.3.0.jar"> <Domain Name="DOIN7L9R"><Application Name="CAC07015108"><WebAppComponent Name="/CAC01000004/servicecache"> <Domain Name="DOIN7L9R"><Application Name="CAC07015108"><test> <Domain Name="DOIN7L9R"><Application Name="CAC07015100"> <Domain Name="DOIN7L9R"><Application Name="CAC07015100"><EJBComponent Name="servicecache-ejb-1.3.0.jar"> <Domain Name="DOIN7L9R"><Application Name="CAC07015100"><WebAppComponent Name="/CAC07015100/servicecache"> #####apres <Domain Name="DOIN7L9R"> <Application Name="CAC07015108" Name3="ff" Name2="ff"> <EJBComponent Name="servicecache-ejb-1.3.0.jar"> </EJBComponent> <WebAppComponent Name="/CAC01000004/servicecache"> </WebAppComponent> <test> </test> </Application> <Application Name="CAC07015100"> <EJBComponent Name="servicecache-ejb-1.3.0.jar"> </EJBComponent> <WebAppComponent Name="/CAC07015100/servicecache"> </WebAppComponent> </Application> </Domain> |