How to Create a New XML File

How to Create a New XML File thumbnail
Create a New XML File

New XML files are generated by programs as a way of housing output data for later querying by the same or a different application. To generate a new XML file from an external program, certain specifications must be met, such as making sure the hierarchical structure adheres to XML standards. The generation and the naming of the new XML file requires that the program use an open() command with the appropriate instructions.

Things You'll Need

  • Text editor, e.g. Notepad, Emacs, ConText
Show More

Instructions

    • 1

      Generate an empty XML file by using the open() command with instructions for appending. This step will also print the XML header and the mother node.
      open(XML,">>info.xml");
      print XML "<\?xml version=\"1.0\" \?>\n";
      print XML "<info>\n";
      The XML file will automatically be called "info.xml" unless you specify otherwise. The '>>' characters instruct that the file is to be used for appending data.

    • 2

      Populate the XML file. These few lines of code solicit information from the user. After one record has been entered, the information automatically populates the XML file through the "print" command.
      TOP:
      print "Name: ";
      $name=<>;
      chomp($name);
      print "Nationality: ";
      $nat=<>;
      chomp($nat);
      print "Date of birth: ";
      $birth=<>;
      chomp($birth);
      print XML "<p_info>\n <name>$name<\/name>\n <nat>$nat<\/nat>\n <birth>$birth<\/birth>\n<\/p_info>\n";
      The "<>;" characters tell the program that the input must come from the user. The chomp(); command gets rid of pesky new line characters that would make the program crash.

    • 3

      Program the input option. After the input is printed to the XML file, the program asks the user if they would like to input a new record. For an affirmative response they must press "n" and if they want to escape the program they must press "e."
      print "For new record press \"n\" -- To exit press \"e\\n";
      print "?: ";
      $new = <>;
      chomp($new);
      if ($new eq "n"){
      goto TOP;
      }
      if ($new eq "e"){
      print XML "<\/info>";
      print "New XML file created";
      exit;
      }
      print "New XML file created";
      This snippet of code also prints the closing tag of the mother node thus making the XML file licit. Save the code as "gen_xml.pl".

    • 4
      Generate new XML File

      Run the program in the Windows command prompt. Open the command prompt and go to the directory where the file is saved, i.e. >cd c:\ your_directory. Once you have entered the directory type "perl gen.xml.pl." Enter the information for three fictitious individuals and then exit the program.

    • 5

      Test the XML validity. After running gen_xml.pl search, search the same directory for an XML file named "info.xml." Open the file in a browser. If the XML opens in a tree format, then the generation of the file has been successful.

Tips & Warnings

  • If there is not a file on disk by the name specified in the open() function, Perl will automatically generate it.

  • If you want to make a second XML file, make sure either to erase the contents of info.xml, or change the name of the new XML file in the open() command.

Related Searches:

References

  • Photo Credit Generate the XML File

Comments

You May Also Like

Related Ads

Featured