aspose file tools*
The moose likes XML and Related Technologies and the fly likes Extract full path when parsing XML Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Extract full path when parsing XML" Watch "Extract full path when parsing XML" New topic
Author

Extract full path when parsing XML

Kevin Stone
Greenhorn

Joined: Oct 23, 2003
Posts: 2
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


Kevin Stone
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

You are only reading the parent of a selected element. But your example appears to say that you want to know all the ancestors of a selected element. So do that. You'll need a loop which gets parents of the parents and so on.
Kevin Stone
Greenhorn

Joined: Oct 23, 2003
Posts: 2
Paul

Thanks for the reply. Wood for trees. Having created a loop for all the ancestors it work.

Once again thanks
 
Don't get me started about those stupid light bulbs.
 
subject: Extract full path when parsing XML