I have my projects.xml file as below:
<projects>
<project>
<id>ID_ABCD1</id>
<name>ptest1</name>
</project>
<project>
<id>IDB_abcd</id>
<name>ptest1</name>
<branch>bnm1</branch>
<branched_from_id>ID_ABCD1</branched_from_id>
<branched_from_version>111</branched_from_version>
</project>
<project>
<id>ID_ABCD2</id>
<name>ptest2</name>
</project>
<project>
<id>IDB_abcd2</id>
<name>ptest2</name>
<branch>bnm2</branch>
<branched_from_id>ID_ABCD2</branched_from_id>
<branched_from_version>111</branched_from_version>
</project>
</projects>
In my existing code I am getting project names based on project ID obtained from another file categories.xml.
For each of these project names I have to search is there any branch element for it, and if the element exists then I have to get
the corresponding branch name and branched_from_version.
branched_from_id value is the ID of some other project.
#!/usr/bin/env perl
use strict;
use XML::LibXML;
use File::Basename;
use File::Find::Rule;
use XML::Twig;
my $cat_xml = '/home/Ras/categories.xml';
my $twig = XML::Twig->parse( $cat_xml );
my $doc = XML::LibXML->new->parse_file( $cat_xml );
my $proj_xml = '/home/Ras/projects.xml';
my $twigp = XML::LibXML->new->parse_file( $proj_xml );
my @nodes = $doc->findnodes( '/categories/category' );
my $catname = "/home/Ras/category.txt";
open FH, "> $catname" or warn "not able to open catname file";
foreach my $catnode ( @nodes ) {
my @catn = $catnode->findvalue( '@name' );
print FH "@catn \n";
}
my @catlist = `cat /home/Ras/category.txt`;
foreach my $cat ( @catlist ) {
chomp( $cat );
$cat =~ s/^\s+|\s$//g;
my $path = "./category[\@name=\"$cat\"]/project";
foreach my $project ( $twig->findnodes( "$path" ) ) {
my $projout = $project->text;
foreach my $projname ( $twigp->findvalue( "/projects/project[id = '$projout']/name" ) ) {
}
}
}
In above code $projout contain the ID value of my project.xml file. But those ID value with branch node are not there.
I have name value in $projname. So based on the name value in $projname I need to get the corresponding branch value and branched_from_version value.
XML::TwigandXML::LibXML? That looks like a road to pain.categorynames fromcategories.xmland write it to disk; then you read it back and search the same xml file for thosecategoryelements! You will need to showcategories.xmlas well if you want help with this.