Results 1 to 1 of 1
- 03-19-2013, 05:27 PM #1
Member
- Join Date
- Oct 2012
- Posts
- 43
- Rep Power
- 0
beginners question - create an element
Hello, just starting to learn how to work with XML via Java and I have encountered an error.
I have an XML file with car owners and their cars.
XML file:
And I want to write a method, that changes the owner of the car, i.e. moves the car element to another person.Java Code:<?xml version="1.0" encoding="utf-8"?> <people> <person id="1"> <!-- id is required, unique (positive) integer (no two cars with same ID) --> <name>Pavel</name> <!-- name is required, non-empty string --> <surname>Novák</surname> <!-- surname is required, non-empty string --> <salary>12000.00</salary> <!-- salary is required, positive number with 2 decimal places precision, e.g. 10000.50 --> <note>Poznámka</note> <!-- note optional element, repeatable, contains any string --> <cars> <!-- cars optional element, contains any number of child-element car --> <car id="ZNC 33-93"> <!-- id is required, format like this: LLL NN-NN where L is a letter, N a ciphers 0..9 two or three letters, space, two ciphers, comma and 2 ciphers) --> <type>Škoda 120 GLX</type> <!-- type is required, non-empty string --> <fuel>gasoline</fuel> <!-- fuel is required, enumeration with values: gasoline/diesel/lpg/electricity --> <year>1985</year> <!-- year is required, 4-ciphers positive integer number --> <country>cz</country> <!-- country is required, 2 chars string --> <price>15000</price> <!-- price is required, positive integer --> </car> </cars> </person> <person id="2" age="21"> <name>Libor</name> <surname>Polák</surname> <salary>9000.00</salary> </person> <person id="3"> <name>Jan</name> <surname>Hroza</surname> <salary>68475.25</salary> <cars> <car id="6B6 82-51"> <type>Škoda Octavia Combi</type> <fuel>UNKNOWN</fuel> <year>2009</year> <country>cz</country> <price>200000</price> </car> <car id="ZNH 28-51"> <type>Škoda Octavia Combi</type> <fuel>diesel</fuel> <year>2001</year> <country>cz</country> <price>50000</price> </car> <car id="ZNE 11-75"> <type>Škoda Favorit</type> <fuel>lpg</fuel> <year>1995</year> <country>cz</country> <price>20000</price> </car> <car id="ZN 85-51"> <type>Prototyp č. 1 vyrobený v dílně Franty Vomáčky</type> <fuel>electricity</fuel> <year>1995</year> <country>cz</country> <price>150000</price> </car> </cars> </person> </people>
Here is what I got so fat, it seems to remove the element, but does not create the new one>
And I can´t really find the problem. If anyone can point me in the right direction, I would be really grateful.Java Code:public class Main { /** * W3C object model representation of a XML document. * Note: We use the interface(!) not its implementation */ private Document doc; /** * Task 1, part A: Complete this method. * * Sells the car identified by carId to the person identified by personId. * (i.e. moves the respective car element into the cars element * of the recipient person, new owner. * Note that if the recipient does not have any cars yet, * the containing cars element must first be created before * the cars is moved into it. * if either the car or the recipient is not present, the method * does not change nor report anything. * * @param carId id of the car to be sold * @param personId person receiving the car * @throws IllegalArgumentException if any parameter is null or empty */ public void sellCarTo(String carId, String personId) { Element car = null; Element person; Element pomCar = null; NodeList carList = doc.getElementsByTagName("car"); for(int i = 0; i < carList.getLength(); i++) { car = (Element) carList.item(i); String pomCarId = car.getAttribute("id"); if(pomCarId.equals(carId)) { String myCarId = pomCarId; pomCar = car; car.getParentNode().removeChild(car); } } NodeList peopleList = doc.getElementsByTagName("person"); for(int i = 0; i < peopleList.getLength(); i++) { person = (Element) peopleList.item(i); String pomPersonId = person.getAttribute("id"); if(pomPersonId.equals(personId)) { Element newCar = null; NodeList cars = person.getElementsByTagName("cars"); if(cars.getLength() == 0) { newCar = doc.createElement("cars"); person.appendChild(pomCar); } else { newCar = (Element) cars.item(carList.getLength() + 1); } newCar.appendChild(pomCar); } } }
Similar Threads
-
beginners error
By sidra 100 in forum New To JavaReplies: 1Last Post: 10-31-2011, 08:07 PM -
Create a Class that instantiates the GUI interface question
By Roastbeef in forum New To JavaReplies: 0Last Post: 10-31-2011, 11:55 AM -
Beginners fault
By NGE in forum New To JavaReplies: 6Last Post: 06-23-2011, 07:28 PM -
Array/Element Question
By Polymer in forum New To JavaReplies: 12Last Post: 09-24-2010, 01:13 AM -
A newbie question: How do I take a J2ME open source and create a project
By Anona in forum CLDC and MIDPReplies: 0Last Post: 09-28-2008, 11:02 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks