Question

Create a program called ReadNames FromXML that reads the file lab10.xml (also in a ZIP file here for download), stores each s

do this in java and as simple as possible as i am new to this.
The lab10.xml is here

_<java version=1.8.0_144 class=java.beans.XML Decoder> <string>Harry</string> <string>Ron</string> <string>Hermione</stri

and the zip file is here

k?xml version=1.0 encoding=UTF-8?> Kjava version=1.8.0_144 class=java.beans.XMLDecoder> <string>Harry</string> <str

Thank you

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find the code and its output below.

 import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilder; import org.w3c.dom.Document; import org.w3c.dom.NodeList; import org.w3c.dom.Node; import org.w3c.dom.Element; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.util.ArrayList; import java.util.List; public class ReadXMLFileExample1 { public static void main(String argv[])throws IOException , FileNotFoundException,ArrayIndexOutOfBoundsException , Exception { try { List<String> ar = new ArrayList<String>(); File file = new File("C:\\Users\\escuhao\\Desktop\\XMLFile.xml"); // Put the path of the XMLFile.xml DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance();//It is an instance that gives a document builder DocumentBuilder build = fact.newDocumentBuilder(); //This is an instance of builder to parse the specified xml file (XMLFile.xml) Document doc = build.parse(file); doc.getDocumentElement().normalize(); NodeList nl = doc.getElementsByTagName("java"); // This is the root element NodeList nl1 = doc.getElementsByTagName("string"); int length = nl.getLength(); int length1 = nl1.getLength(); int j=0 ; System.out.println(length1+" names are found in the .xml file:\n "); for (j = 0; j < length; j++) { Node node = nl.item(j); Element eElement = (Element) node; for (int i=0;i<length1;i++ ) { System.out.println(eElement.getElementsByTagName("string").item(i).getTextContent()); ar.add(eElement.getElementsByTagName("string").item(i).getTextContent()); } System.out.println("\nAll names are stored inside the array : \n" +ar); } } catch (FileNotFoundException e) { System.err.println("File not found "); } catch (ArrayIndexOutOfBoundsException e) { System.err.println("Array index out of bounds !!!"); } catch (Exception e) {     System.err.println("An exception occurs"); } } }

OUTPUT:

<terminated> ReadXMLFile Example 1 [Java Application] C:\Program FilesVava\jdk-12.0.2\bin\javaw.exe (r 10 names are found in

If the required file is not found then we wil get the below message.

10USD 11 - LISENSIILOOTUS 26 int length = nl.getLength(); 27 int length1 = n11.getLength(); Problems @ Javadoc Declaration Co

Add a comment
Know the answer?
Add Answer to:
do this in java and as simple as possible as i am new to this. The...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • using java Develop the classes and interface represented in the following UML diagram: <cinterface >> Passenger...

    using java Develop the classes and interface represented in the following UML diagram: <cinterface >> Passenger 1 String name has passengers String getNamel) String getFaro Typel) String getName() void display Passengers Person String frame String last Name intage Note that the Person class includes both a first and last name, but the Passenger interface requires a full name. The field fare Type is determined by a passenger's age: "Regular fare is the full fare, "Youth" is a reduced fare for...

  • java This lab is intended to give you practice creating a class with a constructor method,...

    java This lab is intended to give you practice creating a class with a constructor method, accessor methods, mutator methods, equals method , toString method and a equals method. In this lab you need to create two separate classes, one Student class and other Lab10 class. You need to define your instance variables, accessor methods, mutator methods, constructor, toString method and equals method in Student class. You need to create objects in Lab10 class which will have your main method...

  • Copy the file, HP.txt1 , on the course web page to Readme.txt in your BlueJ project....

    Copy the file, HP.txt1 , on the course web page to Readme.txt in your BlueJ project. Develop and test a Java program, AnalyzeText, that reads Readme.txt and reports the following: a) The 3 most common words that are not in this stop-list: a,am,an,and,any,are,as,a,be,by,he,her,hers,him,his,i,if,in,into,is,it,its, me,my,no,nor,not,of,on,or,she,than,that,the,their,them,then,there,these, they,to,too,us,was,we,were,what,when,where,which,while,who,whom,why,you An example of output: Word Frequency wand 21 potion 10 wizard 9 b) The 3 most common names that are in this list of names: Harry, Dumbledore, Voldemort, Snape, Sirius, Hermione, Ron, Draco, Hagrid, Neville,...

  • I need help debugging this Java program. I am getting this error message: Exception in thread...

    I need help debugging this Java program. I am getting this error message: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2 at population.Population.main(Population.java:85) I am not able to run this program. ------------------------------------------------------------------- import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Scanner; /* Linked list node*/ class node { long data; long year; String country; node next; node(String c,long y,long d) { country=c; year=y; data = d; next = null; } } public class Population { private static node head; public static void push(String...

  • Need help with java programming. Here is what I need to do: Write a Java program...

    Need help with java programming. Here is what I need to do: Write a Java program that could help test programs that use text files. Your program will copy an input file to standard output, but whenever it sees a “$integer”, will replace that variable by a corresponding value in a 2ndfile, which will be called the “variable value file”. The requirements for the assignment: 1.     The input and variable value file are both text files that will be specified in...

  • I'm working with Java and I have a error message  1 error Error: Could not find or...

    I'm working with Java and I have a error message  1 error Error: Could not find or load main class Flowers. This is the problem: Instructions Make sure the source code file named Flowers.java is open. Declare the variables you will need. Write the Java statements that will open the input file, flowers.dat, for reading. Write a while loop to read the input until EOF is reached. In the body of the loop, print the name of each flower and where...

  • signature 1. Create a new NetBeans Java project. The name of the project has to be...

    signature 1. Create a new NetBeans Java project. The name of the project has to be the first part of the name you write on the test sheet. The name of the package has to be testo You can chose a name for the Main class. (2p) 2. Create a new class named Address in the test Two package. This class has the following attributes: city: String-the name of the city zip: int - the ZIP code of the city...

  • Programming Project 3 See Dropbox for due date Project Outcomes: Develop a Java program that uses:...

    Programming Project 3 See Dropbox for due date Project Outcomes: Develop a Java program that uses: Exception handling File Processing(text) Regular Expressions Prep Readings: Absolute Java, chapters 1 - 9 and Regular Expression in Java Project Overview: Create a Java program that allows a user to pick a cell phone and cell phone package and shows the cost. Inthis program the design is left up to the programmer however good object oriented design is required.    Project Requirements Develop a text...

  • Hi, I am writing Java code and I am having a trouble working it out. The...

    Hi, I am writing Java code and I am having a trouble working it out. The instructions can be found below, and the code. Thanks. Instructions Here are the methods needed for CIS425_Student: Constructor: public CIS425_Student( String id, String name, int num_exams ) Create an int array exams[num_exams] which will hold all exam grades for a student Save num_exams for later error checking public boolean addGrade( int exam, int grade ) Save a grade in the exams[ ] array at...

  • Using Java; Consider the following text file format. Ruminant Shaman 30 Elentriel Shaman 70 Aelkine Druid...

    Using Java; Consider the following text file format. Ruminant Shaman 30 Elentriel Shaman 70 Aelkine Druid 29 Barbatus Druid 70 Ukaimar Mage 47 Mariko Priest 33 Farmbuyer Priest 70 Valefar Warlock 42 Teslar Paladin 64 Nerdsbane Hunter 12 We wish to store information regarding several characters in a video game. Each character has a name, and a class, and a level. This information is repeated, 3 lines per character, for any number of characters. To simplify processing, the very first...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT