I new to java and XML. My goal is to get info from my XML file and save it to a string array for later use in my code for testing my website. The XML contains elements of each page divided to 3 categories: Name, Attribute, Text. My first step was just to isolate the data i want and print it, and already got stuck.
Here is an example for my XML file (the original has a lot more nodes using the same structure):
<?xml version="1.0" encoding=""ISO-8859-1""?>
<config>
<HomeScreenName>
<Logo>Logo</Logo>
<Mainimage>Main image</Mainimage>
<Maintext>Main text</Maintext>
<Backupbutton>Backup button</Backupbutton>
<ViewBackupbutton>View Backup button</ViewBackupbutton>
<Version>Version</Version>
<Cancelaccountbutton>Cancel account button</Cancelaccountbutton>
</HomeScreenName>
<HomeScreenAttributes>
<Logo>/html/body/div[1]/div[1]</Logo>
<Mainimage>//*[@id="img-content"]</Mainimage>
<Maintext>/html/body/div[1]/div[3]/h3</Maintext>
<Backupbutton>/html/body/div[1]/div[3]/div[1]/a/span</Backupbutton>
<ViewBackupbutton>/html/body/div[1]/div[3]/div[2]/a/span</ViewBackupbutton>
<Version>//*[@id="version"]</Version>
<Cancelaccountbutton>//*[@id="unregister"]/p</Cancelaccountbutton>
</HomeScreenAttributes>
<HomeScreenText>
<Logo />
<Mainimage />
<Maintext>Secure backup</Maintext>
<Backupbutton>Back Up</Backupbutton>
<ViewBackupbutton>View Your Backups</ViewBackupbutton>
<Version>Version 1.0.3</Version>
<Cancelaccountbutton />
</HomeScreenText>
</config>
From this XML i would like to create 4 arrays: First with just the name of each node: array1 = [HomeScreenName, HomeScreenAttributes, HomeScreenText]
Then i want an array for each node attributes: something like this: array2(from HomeScreenName) = [Logo, Main image, Main text, Backup button, View Backup button, Version, Cancel account button]
I have 2 main problems:
How to get just the data i want and not everything from the XML.
How to save the data (I wanted arrays, but i'm open to suggstions).
Here is the code I have for printing every node in the XML file:
import java.io.File;
import java.io.IOException;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
public class readConfigXML{
public static void main(String[] args)
{
SAXBuilder builder = new SAXBuilder();
String folderPath = "C:\\Users\\udi\\Documents\\external\\XML\\";
String fileName = "configTest.xml";
String filePath = folderPath + fileName;
File xmlFile = new File(filePath);
try {
Document document = (Document) builder.build(xmlFile);
Element rootNode = document.getRootElement();
List configList = rootNode.getChildren();
for (int i = 0; i < configList.size(); i++)
{
Element node = (Element) configList.get(i);
List dataNodes = node.getChildren();
for (int j = 0; j < dataNodes.size(); ++j)
{
Element dataNode = (Element) dataNodes.get(j);
System.out.println(dataNode.getName());
}
}
}
catch (IOException io)
{
System.out.println(io.getMessage());
}
catch (JDOMException jdomex)
{
System.out.println(jdomex.getMessage());
}
}
}
Any help will be greatly appreciated!