Hello,
How can I access the value for each attribute? can anyone show me? Below I have xml file and code which executes ok starting from the root to each element.
/*
<?xml version="1.0" ?>
- <catalog>
- <title>
<name>xyz</name>
<description>xyz Fundamentals</description>
<author>Jason</author>
<rating>4</rating>
<available>Yes</available>
</title>
- <title>
<name>Applied XML</name>
<description>XML 101</description>
<author>Jason</author>
<rating>5</rating>
<available>Yes</available>
</title>
</catalog>
*/
public void ModifyXMLData()
{
FileReader fileReader = null;
BufferedReader xmlReader = null;
try
{
FileWriter modifyTxtFile = new FileWriter("\\modifyTxtFile.txt");
BufferedWriter writeBuffer = new BufferedWriter(modifyTxtFile);
fileReader = new FileReader("\\book.xml");
xmlReader = new BufferedReader(fileReader);
parser = new KXmlParser();
parser.setInput(xmlReader);
//Create the XML Document structure
Document doc = new Document();
doc.parse(parser);
XmlSerializer serializer = new KXmlSerializer();
Element root = doc.getRootElement();
int children = root.getChildCount();
writeBuffer.write("root= " + root.getName());//catalog
writeBuffer.write("\r\n");
writeBuffer.write("#children= " + Integer.toString(children));//5
writeBuffer.write("\r\n");
Element title = root.getElement(1);
writeBuffer.write("getElement= " + title.getName());//title
writeBuffer.write("\r\n");
Element name = (Element)title.getChild(1);
writeBuffer.write("getElement= " + name.getName());//name
writeBuffer.write("\r\n");
Element elDescription = title.getElement("", "description");
writeBuffer.write("getElement= " + elDescription.getName());//description
writeBuffer.write("\r\n");
Element elAuthor = title.getElement("", "author");
writeBuffer.write("getElement= " + elAuthor.getName());//author
writeBuffer.write("\r\n");
Element elRate = title.getElement("", "rating");
writeBuffer.write("getElement= " + elRate.getName());//rate
writeBuffer.write("\r\n");
elRate.removeChild(0);
Element elAvailable = title.getElement("", "available");
writeBuffer.write("getElement= " + elAvailable.getName());//available
writeBuffer.write("\r\n");
writeBuffer.close();
}
catch(Exception error)
{
errorMsg = error.getMessage();
}
}
From the xml file, I have tag for 'name', but what if I want to get the value for example xyz, how can I get it? because I want to modify it, but to modify it, I need to know that I get a correct element but I can't get the attribute value for the 'name' element.
thanks