Hi All,
Im attempting to write a xml based authentication java bean, the xml files is structured as follows:
<users>
<user>
<username>Admin</username>
<password>Test</password>
<firstname>Bob</firstname>
</user>
<user>
<username>User</username>
<password>Test</password>
<firstname>Jim</firstname>
</user>
</user>
Theres more information for each user, but you get the idea.
I've got the following code, I think it'll loop through the <user></user> elements ? but then im unsure howto delve deeper into the element and get at its sub elements?
Also i've found that I need to use org.w3c.dom.Text to get at the text of a element, any examples of this?
Thanks,
Heres the code I currently have:
// Create URL instance to path
URL url = new URL(path);
// Create JAXP document builder factory
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
// Create JAXP document builder
DocumentBuilder parser = factory.newDocumentBuilder();
// Retrieve stream from xml file
InputStream stream = url.openStream();
// Parse xml data
Document xml = parser.parse(stream);
// Generate node list from xml document
NodeList nodes = xml.getElementsByTagName("User");
// Loop through user nodes
for (int i = 0; i < nodes.getLength(); i++)
{
// Reference current element
Element element = (Element)nodes.item(i);
}
This is using the org.w3c.dom document object models and javax.parsers document builder.
Any help/advice would be greatly appreciated, Thanks,
Matt.
Im attempting to write a xml based authentication java bean, the xml files is structured as follows:
<users>
<user>
<username>Admin</username>
<password>Test</password>
<firstname>Bob</firstname>
</user>
<user>
<username>User</username>
<password>Test</password>
<firstname>Jim</firstname>
</user>
</user>
Theres more information for each user, but you get the idea.
I've got the following code, I think it'll loop through the <user></user> elements ? but then im unsure howto delve deeper into the element and get at its sub elements?
Also i've found that I need to use org.w3c.dom.Text to get at the text of a element, any examples of this?
Thanks,
Heres the code I currently have:
// Create URL instance to path
URL url = new URL(path);
// Create JAXP document builder factory
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
// Create JAXP document builder
DocumentBuilder parser = factory.newDocumentBuilder();
// Retrieve stream from xml file
InputStream stream = url.openStream();
// Parse xml data
Document xml = parser.parse(stream);
// Generate node list from xml document
NodeList nodes = xml.getElementsByTagName("User");
// Loop through user nodes
for (int i = 0; i < nodes.getLength(); i++)
{
// Reference current element
Element element = (Element)nodes.item(i);
}
This is using the org.w3c.dom document object models and javax.parsers document builder.
Any help/advice would be greatly appreciated, Thanks,
Matt.