Creating random XML entries in Java or Perl
I am looking for a method to insert random XML data in Java (or Perl).
&l开发者_Python百科t;vehicles ID="DJ3RKFF9">
<vehicle>
<make>Toyota</make>
<model>Yaris</model>
<year>2009</year>
</vehicle>
<vehicle>
<make>Ford</make>
<model>Taurus</model>
<year>2008</year>
</vehicle>
</vehicles>
In the above example, I would need to make the ID
attribute a random series of characters. The rest of the data can remain the same.
It is only the insertion that I need help with, not creating the strings (thanks Sinan Ünür!)
Thanks in advance.
So, basically, your question is how can I generate an eight character random string consisting of digits and capital letters?:
#!/usr/bin/perl
use strict; use warnings;
my $tmpl = <<EO_XML;
<vehicles ID="%s">
<vehicle>
<make>Toyota</make>
<model>Yaris</model>
<year>2009</year>
</vehicle>
<vehicle>
<make>Ford</make>
<model>Taurus</model>
<year>2008</year>
</vehicle>
</vehicles>
EO_XML
my $generator = make_id_generator('A' .. 'Z', 0 .. 9);
printf $tmpl, $generator->() for 1 .. 10;
sub make_id_generator {
my @chars = @_;
return sub {
join '', @chars[ map { rand @chars } 1 .. 8 ];
}
}
Although there's nothing wrong with the templating solution, I want to show that it's not necessarily hard to do it in a "proper" XML-slinging kind of way, and it might save you a lot of time later on if your requirements ever change.
That said, here's a sketch of how you might build this up with XML::Twig. Any corners that I've cut are strictly in the name of producing a nice example snippet that fits on one screen :)
use strict;
use warnings;
use XML::Twig;
sub gen_id {
my @chars = ('A'..'Z', '0'..'9');
join '', @chars[ map { rand @chars } 1 .. 8 ];
}
sub gen_vehicle {
my $vehicle = XML::Twig::Elt->new('vehicle');
while (my ($key, $value) = splice @_, 0, 2) {
XML::Twig::Elt->new($key => $value)
->paste(last_child => $vehicle);
}
$vehicle;
}
sub gen_vehicles {
my $vehicles = XML::Twig::Elt->new('vehicles');
$vehicles->set_att( ID => gen_id() );
gen_vehicle(make => 'Toyota', model => 'Yaris', year => 2009)
->paste(last_child => $vehicles);
gen_vehicle(make => 'Ford', model => 'Taurus', year => 2008)
->paste(last_child => $vehicles);
$vehicles;
}
XML::Twig->set_pretty_print('indented');
gen_vehicles()->print;
Much as it pains me to solve XML problem using regular expressions, in this case we can probably make an exception:
$ perl -ipe '{my $string = generate_random_string(); s/<vehicles ID="DJ3RKFF9">/<vehicles ID="$string">/;}' myfile.xml
a random string generator can be found by googling "perl random string", for example this: http://guymal.com/mycode/generate_random_string/
To do this in Java you can use DOM: This will add the attribute value to an xml file (xmlInput) which doesn't have the ID attribute set
DocumentBuilderFactory dbFac = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = dbFac.newDocumentBuilder();
Document doc = docBuilder.parse("resources/xmlinput.xml");
NodeList vehicles = doc.getElementsByTagName("vehicles");
NamedNodeMap attributes = vehicles.item(0).getAttributes();
Node ID = attributes.getNamedItem("ID");
String randomString = createRandomString();
ID.setNodeValue(randomString);
XMLSerializer serializer = new XMLSerializer();
serializer.setOutputCharStream(
new java.io.FileWriter("resources/xmloutput.xml")
);
serializer.serialize(doc);
I left out the implementation for createRandomString() but that part should be easy
精彩评论