Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java Programming Problem (please type the code. thanks!): Hollywood likes to mak

ID: 3884140 • Letter: J

Question

Java Programming Problem (please type the code. thanks!):

Hollywood likes to make remakes of movies. Unfortunately, this means our DVD collection may have duplicate titles of movies that were released in different years. We will use the dvd.xml and dvd.dtd presented in the commentary.

To distinguish these remakes from the original, we will need to add a new field called release_year and modify the dvd.dtd accordingly.

Afterwards, write an XML parser (either in DOM or StAX) that will count the number of movies released by decade.

Add a few more DVD entries to our DVD collection XML file collection to verify your code works.

Print the result with the following example XML format:

<DVD>
<summary>
   <count decade="1980">2</count>
   <count decade="1990">10</count>
   <count decade="2000">13</count>
</summary>
</DVD>


Note: Don't worry about writing a DTD for the summary XML file.

Explanation / Answer

Tested on Eclipse

/**********************************DomParserDemo.java********************************/

import java.io.File;

import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;

import org.w3c.dom.Element;

import org.w3c.dom.NamedNodeMap;

import org.w3c.dom.Node;

import org.w3c.dom.NodeList;

/**

* The Class DomParserDemo.

*/

public class DomParserDemo {

/**

* The main method.

*

* @param args the arguments

*/

public static void main(String[] args) {

try {

File inputFile = new File("input.txt");

DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();

DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();

Document doc = dBuilder.parse(inputFile);

doc.getDocumentElement().normalize();

System.out.println("Root element :" + doc.getDocumentElement().getNodeName());

NodeList nList = doc.getElementsByTagName("summary");

System.out.println("----------------------------");

for (int temp = 0; temp < nList.getLength(); temp++) {

Node nNode = nList.item(temp);

System.out.println(" Current Element :" + nNode.getNodeName());

if (nNode.getNodeType() == Node.ELEMENT_NODE) {

Element eElement = (Element) nNode;

//Reading Elements by Tag name as count

NodeList fieldNodes = eElement.getElementsByTagName("count");

for(int j = 0; j < fieldNodes.getLength(); j++) {

Node fieldNode = fieldNodes.item(j);

NamedNodeMap attributes = fieldNode.getAttributes();

// reading Attributes of Field Nodes

Node attr = attributes.getNamedItem("decade");

System.out.print("Movie Count in year: ");

if(attr != null) {

System.out.print(attr.getTextContent());

}

System.out.println(" is "+fieldNode.getTextContent());

}

}

}

} catch (Exception e) {

e.printStackTrace();

}

}

}

/*********************************Output**************************/

Root element :DVD
----------------------------

Current Element :summary
Movie Count in year: 1980 is 2
Movie Count in year: 1990 is 10
Movie Count in year: 2000 is 13

/***************************input.txt*********************/

<?xml version = "1.0"?>
<DVD>
<summary>
<count decade="1980">2</count>
<count decade="1990">10</count>
<count decade="2000">13</count>
</summary>
</DVD>

Thanks a lot. Please let me know if you have any doubt.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote