Hi
I am trying to parse an XML with the aim of producing the full path for each element.
Sample XML;
<
TEST>
<AREA>10</AREA>
<XSLT_VERSION>1</XSLT_VERSION>
<SALES_NAME>Tractor Farm</SALES_NAME>
<SALES_DETAILS>
<SALES_LIST>20</SALES_LIST>
<SALES_RECORD>
<RECORD_TYPE>On-line</RECORD_TYPE>
<CREATED>2010-06-4 21:41</CREATED>
</SALES_RECORD>
<SALES_ID>A51234</SALES_ID>
</SALES_DETAILS>
</TEST>
Sample
java;
package SamplePackage;
import java.io.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
public class Sample {
static public void main(
String[] args) {
String[] elementName = new String[10];
int indx = 0;
boolean parent = false;
String parentNameG = null;
String parentNameG2 = null;
String[] parentName = new String[5];
Element elementP = null;
try {
File file = new File("test.xml");
if (file.exists()) {
// Create dom factory
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
// Use the factory to create a document builder
DocumentBuilder builder = factory.newDocumentBuilder();
Document
doc = builder.parse(file);
// Get a list of all elements in the document
NodeList list = doc.getElementsByTagName("*");
// Loop through elements
for (int i = 0; i < list.getLength(); i++) {
// Get element
Element element = (Element) list.item(i);
NodeList children = element.getChildNodes();
// Loop through child nodes
if (element.hasChildNodes()) {
for (int ii = 0; ii < children.getLength(); ii++) {
Node textChild = children.item(ii);
// We only want to get nodes with values
if ((textChild.getNodeType() == Node.TEXT_NODE)
&& !(textChild.getNodeValue() == null)
&& !(textChild.getNodeValue().trim().isEmpty())) {
// Get the parent node
parentNameG = element.getParentNode().toString();
// Tidy parent node up
parentName = parentNameG.split(":");
System.out.println(parentName[0].substring(1) + "/" + element.getNodeName());
// System.out.println("\n" + textChild.getNodeValue());
}
}
}
}
} else {
System.out.print("File not found!");
}
} catch (Exception e) {
System.out.println(e);
System.exit(1);
}
}
}
If I run the attached code with the sample XML I get the following result;
TEST/AREA
TEST/XSLT_VERSION
TEST/SALES_NAME
SALES_DETAILS/SALES_LIST
SALES_RECORD/RECORD_TYPE
SALES_RECORD/CREATED
SALES_DETAILS/SALES_ID
What I really want is the following;
TEST/AREA
TEST/XSLT_VERSION
TEST/SALES_NAME
SALES_DETAILS/SALES_LIST
SALES_DETAILS/SALES_RECORD/RECORD_TYPE
SALES_DETAILS/SALES_RECORD/CREATED
SALES_DETAILS/SALES_ID
Can any help me please to modify the code, and yes I know the code is untidy, I have been trying various things to no avail.
Thanks