• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Extract full path when parsing XML

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Paul

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

Once again thanks
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic