Parsing an XML file with JDOM, error StackOverflowError - java

im trying to get all element of an XML file and put it into a ArrayList>> with a recursive method, but i get an error : Exception in thread "main" java.lang.StackOverflowError at java.util.ArrayList.
the error is getting when i make a recursive call : GetAllXml(ListTree);
and i want to get a strcuture like this [[[un]] , [[deux,trois,quatre]] , [[cinq,six,sept],[huit,noeuf],[dix,onze]]]
here is my code:
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.NodeList;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
public class esperant {
/**
* #param args
*/
private static List<Element> getChildren(Node parent)
{
NodeList nl = parent.getChildNodes();
List<Element> children = new ArrayList<Element>(nl.getLength());
for (int i = 0; i < nl.getLength(); i++) {
Node n = nl.item(i);
if (n instanceof Element)
children.add((Element) n);
}
return children;
}
public static void GetAllXml(ArrayList<ArrayList<ArrayList<Element>>> ListTree)
{
ArrayList<ArrayList<Element>> child = new ArrayList<ArrayList<Element>>();
int level = ListTree.size()-1;
for (int i=0;i<ListTree.get(level).size();i++)
{
for (int j=0;j<ListTree.get(level).get(i).size();j++)
{
ArrayList<Element> childOfChild = new ArrayList<Element>();
childOfChild.addAll(getChildren(ListTree.get(level).get(i).get(j)));
child.add(childOfChild);
}
}
ListTree.add(child);
GetAllXml(ListTree);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayList<ArrayList<ArrayList<Element>>> ListTree = new ArrayList<ArrayList<ArrayList<Element>>>();
ArrayList<ArrayList<Element>> child = new ArrayList<ArrayList<Element>>();
ArrayList<Element> childOfChild = new ArrayList<Element>();
try{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder parser = factory.newDocumentBuilder();
Document doc = parser.parse("test.xml");
Element root = doc.getDocumentElement();
childOfChild.add(root);
child.add(childOfChild);
ListTree.add(child);
GetAllXml(ListTree);
System.out.println(ListTree);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
here is the xml file :
<?xml version="1.0" encoding="iso-8859-1"?>
<un>
<deux> <cinq></cinq> <six></six> <sept></sept> </deux>
<trois> <huit></huit><noeuf></noeuf> </trois>
<quatre><dix></dix><onze></onze> </quatre>
</un>

Change Your GetAllXml(X) like this, it would work. As Every one above said , there is no way out from this method.
final ArrayList<ArrayList<Element>> child = new ArrayList<ArrayList<Element>>();
final int level = ListTree.size() - 1;
for (int i = 0; i < ListTree.get(level).size(); i++)
{
for (int j = 0; j < ListTree.get(level).get(i).size(); j++)
{
final ArrayList<Element> childOfChild = new ArrayList<Element>();
childOfChild.addAll(getChildren(ListTree.get(level).get(i).get(j)));
if (childOfChild.size() > 0)
{
child.add(childOfChild);
}
}
}
if (child.size() > 0)
{
ListTree.add(child);
GetAllXml(ListTree);
}

Every call on GetAllXml(X), whatever else it does, ends up calling GetAllXml(X) passing the same value as its argument. So it's obvious that it will recurse infinitely.

Related

Write excel data into List

I created below class with getter and setter.
import java.util.List;
public class DMNDetails {
private List<String> inputParam;
private List<String> outputParam;
public List<String> getInputParam() {
return inputParam;
}
public void setInputParam(List<String> inputParam) {
this.inputParam = inputParam;
}
public List<String> getOutputParam() {
return outputParam;
}
public void setOutputParam(List<String> outputParam) {
this.outputParam = outputParam;
}
#Override
public String toString() {
return ("Inputname: "+ getInputParam()+
" Outputname: "+ getOutputParam());
}
}
Extracting input and output variables from XML and return using List.
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class ExtractingValues {
public DMNDetails Param() {
List<String> inputval = new ArrayList<String>();
DMNDetails dtls = new DMNDetails();
String variables;
// String dataType;
try {
File file = new File("C:/test.xml");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(file);
doc.getDocumentElement().normalize();
// System.out.println("Root element: "+ doc.getDocumentElement().getNodeName());
NodeList nodeList = doc.getElementsByTagName("inputExpression");
for (int i = 0; i < nodeList.getLength(); ++i) {
Node node = nodeList.item(i);
// System.out.println("\nNode Name :" + node.getNodeName());
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element tElement = (Element)node;
variables = tElement.getElementsByTagName("text").item(0).getTextContent();
// dataType = tElement.getAttribute("typeRef");
// System.out.println("Variable: "+ variables);
// System.out.println("Type: "+ dataType);
switch(i) {
case 0:
inputval.add(variables);
// System.out.println("Input1: "+dtls1.getInput1());
break;
case 1:
inputval.add(variables);
// System.out.println("Input2: "+dtls2.getInput2());
break;
case 2:
inputval.add(variables);
// System.out.println("Input3: "+dtls3.getInput3());
break;
default:
System.out.println("Error");
}
}
}
}
catch (Exception e) {
System.out.println(e);
}
dtls.setInputParam(inputval);
/*** OutputVariable Section ***/
List<String> outputval = new ArrayList<String>();
String variablesout;
// String dataType;
try {
File file = new File("C:/Jira_task/Sprint_17/Business.xml");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(file);
doc.getDocumentElement().normalize();
// System.out.println("Root element: "+ doc.getDocumentElement().getNodeName());
NodeList nodeList2 = doc.getElementsByTagName("output");
for (int i = 0; i < nodeList2.getLength(); ++i) {
Node node2 = nodeList2.item(i);
// System.out.println("\nNode Name :" + node2.getNodeName());
if (node2.getNodeType() == Node.ELEMENT_NODE) {
Element tElement = (Element)node2;
variablesout = tElement.getAttribute("name");
// dataType = tElement.getAttribute("typeRef");
// System.out.println("Variable: "+ variables);
// System.out.println("Type: "+ dataType);
switch(i) {
case 0:
outputval.add(variablesout);
// System.out.println("Output1: "+dtls1.getOutput1());
break;
case 1:
outputval.add(variablesout);
// System.out.println("Output2: "+dtls2.getOutput2());
break;
case 2:
outputval.add(variablesout);
// System.out.println("Output3: "+dtls3.getOutput3());
break;
default:
System.out.println("Error");
}
}
}
}
catch (Exception e) {
System.out.println(e);
}
dtls.setOutputParam(outputval);
return dtls;
}
}
Here we are get data from "TestCase.xlsx" excel and store in data list and return. My expectation are written inside commented block inside.
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ReadExcelTestCase2 {
public static List<DMNRow> getdata() throws IOException {
List<DMNRow> data = new ArrayList<>();
// Reading file from local directory
FileInputStream file = new FileInputStream("C:\\TestCase.xlsx");
// Create Workbook instance holding reference to .xlsx file
XSSFWorkbook XS = new XSSFWorkbook(file);
int numberofsheets = XS.getNumberOfSheets();
for (int i = 0; i < numberofsheets; i++) {
if (XS.getSheetName(i).equalsIgnoreCase("Sheet1")) {
XSSFSheet XSS = XS.getSheetAt(i);
Iterator<Row> r = XSS.iterator();
/* Here input and output variables are hardcode, but cannot able to hardcode value here, because each xml file contain
n number of input and output. So according to n number of variables we need to extract data from excel and store data
in "data". Datatype also change for each variables.
String cr;
long lc;
String arn;
DMNRow DMNRow;
while (r.hasNext()) {
Row row = r.next();
if (row.getRowNum() == 0) {
continue;
}
cr = row.getCell(0).getStringCellValue();
lc = (long) row.getCell(1).getNumericCellValue();
DataFormatter formatter = new DataFormatter();
arn = formatter.formatCellValue(row.getCell(2));
DMNRow = new DMNRow(cr, lc, arn);
data.add(DMNRow);
*/
}
}
}
// Closing file output streams
XS.close();
file.close();
return data;
}
}
After data from excel store and return to another method to compare data with DMN engine and write status in same excel in Result column.
Can anyone please guide how to store data from excel into list to use by another method for compare.

Modifying value of a particular tag in xml

I have an xml as follows
<?xml version="1.0" encoding="ISO-8859-1"?><TXNEXP FileDate="2017-05-23" FileName="/cortex/tsd/out/OPTSKRtxnexp20170523.xml" Instcode="SKR" TotNumTxns="74330">
<AUTHADV>
<LOCALDATE>2017-05-22</LOCALDATE>
<LOCALTIME>200011</LOCALTIME>
<PAN>336890380<PAN>
</AUTHADV>
<AUTHREV>
<LOCALDATE>2017-05-22</LOCALDATE>
<LOCALTIME>200011</LOCALTIME>
<PAN>336890380<PAN>
</AUTHREV>
<FINAL>
<LOCALDATE>2017-05-22</LOCALDATE>
<LOCALTIME>200011</LOCALTIME>
<PAN>336890380<PAN>
</FINAL>
</TXNEXP>
Now, I am modifying the value of PAN tag and writing it back to the xml but I am not able to do so for all the PAN tags.
Here is what I am doing.
NodeList node = doc.getElementsByTagName("TXNEXP");
Element emp = null;
for (int i = 0; i < node.getLength(); i++) {
emp = (Element) node.item(i);
Node name = emp.getElementsByTagName("PAN").item(0).getFirstChild();
//Modifying the tag
}
From the above code only PAN under AUTHADV tag gets modified and the rest two values don't change.
How can I ensure all the PAN tags to get modified ?
This is not the prettiest solution but after you fix the missing slash in the PAN closing tags this will work.
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import java.io.File;
class Extract {
public static void main(String[] args) {
try {
File fXmlFile = new File("data.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
NodeList topNodes = doc.getElementsByTagName("TXNEXP");
for (int i = 0; i < topNodes.getLength(); i++) {
NodeList middleNodes = topNodes.item(i).getChildNodes();
for (int j = 0; j < middleNodes.getLength(); j++) {
try {
NodeList theNodes = ((Element)middleNodes.item(j)).getElementsByTagName("PAN");
System.out.println(theNodes.item(0).getFirstChild().getNodeValue());
if (j == 1) {
// modify a value
theNodes.item(0).getFirstChild().setNodeValue("4567");
System.out.println(theNodes.item(0).getFirstChild().getNodeValue());
}
} catch (ClassCastException e) {}
}
}
} catch (Exception e) {
System.out.println(e);
}
}
}
For a slightly better approach you could use XPaths.
import javax.xml.xpath.*;
class Extract {
public static void main(String[] args) {
try {
File fXmlFile = new File("data.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
XPath xpath = XPathFactory.newInstance().newXPath();
NodeList nodes = (NodeList)xpath.evaluate("/TXNEXP/*/PAN", doc, XPathConstants.NODESET);
for (int n = 0; n < nodes.getLength(); n++) {
System.out.println(nodes.item(n).getFirstChild().getNodeValue());
if (n == 1) {
nodes.item(n).getFirstChild().setNodeValue("4567");
System.out.println(nodes.item(n).getFirstChild().getNodeValue());
}
}
} catch (Exception e) {
System.out.println(e);
}
}
}

writing string to file in java

Below two classes. I have a problem with writing string to file in java. Why in my file xml.txt I get null? Why I can't write String a = px.readXml(url) ? In xml.txt I've only null
package xml;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.*;
import java.util.StringTokenizer;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.SAXException;
public class PrintXml {
public String readXml(URL url) throws ParserConfigurationException, MalformedURLException, IOException, SAXException
{
//URL url = new URL("http://www.nbp.pl/kursy/xml/a093z150515.xml");
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(url.openStream());
Element root = doc.getDocumentElement();
//System.out.println(root.getTagName());
NodeList children = root.getChildNodes();
int liczbaLinijek = children.getLength();
for(int i = 0; i<children.getLength();i++)
{
Node child = children.item(i);
if (child instanceof Element)
{
Element childElement = (Element)child;
NodeList childrenPozycja = childElement.getChildNodes();
for (int j = 0; j<childrenPozycja.getLength(); j++)
{
Node childPozycja = childrenPozycja.item(j);
if (childPozycja instanceof Element)
{
String nameChf = "CHF";
Double kurs;
Element childPozycjaElement = (Element) childPozycja;
String listaKursow = childPozycjaElement.getTextContent();
//System.out.println(listaKursow);
}
}
}
}
return null;
}
public String writeXml(String toFile) throws IOException
{
PrintWriter out = new PrintWriter(new FileWriter("xml.txt"));
out.println(toFile);
out.close();
return null;
}
}
and here is testing class:
package xml;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.*;
import java.util.StringTokenizer;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.SAXException;
public class PrintXmlTester {
public static void main(String[] args) throws MalformedURLException,
ParserConfigurationException, SAXException, IOException {
URL url = new URL("http://www.nbp.pl/kursy/xml/a093z150515.xml");
PrintXml px = new PrintXml();
String a = px.readXml(url);
px.writeXml(a);
}
}
}
To return value of listaKursow you can add return statement in readXml() like below:
public String readXml(URL url) throws ParserConfigurationException, MalformedURLException, IOException, SAXException
{
//...
//Declare listaKursow here
String listaKursow = "";
for(int i = 0; i<children.getLength();i++)
{
//..
for (int j = 0; j<childrenPozycja.getLength(); j++)
{
Node childPozycja = childrenPozycja.item(j);
if (childPozycja instanceof Element)
{
String nameChf = "CHF";
Double kurs;
Element childPozycjaElement = (Element) childPozycja;
String listaKursowTemp = childPozycjaElement.getTextContent();
//System.out.println(listaKursow);
//return value of listaKursow
listaKursow = listaKursow + listaKursowTemp;
}
}
}
return listaKursow;
}
However you should note that this would return first value of listaKursow in Xml.
If you are looking for all values of elements as String, you can add them to a List and return list.toString() or concatenate String variable in each iteration.
EDIT: Based on your inputs I have modified my post to return all lists
#hitz answer is good, but I would suggest using StringBuilder for this. It is far more efficient and nicer to look at.
public String readXml(URL url) throws ParserConfigurationException, MalformedURLException, IOException, SAXException
{
//...
//Declare listaKursow here
final StringBuilder listaKursow = new StringBuilder();
for(int i = 0; i<children.getLength();i++)
{
//..
for (int j = 0; j<childrenPozycja.getLength(); j++)
{
final Node childPozycja = childrenPozycja.item(j);
if (childPozycja instanceof Element)
{
final String nameChf = "CHF";
Double kurs;
final Element childPozycjaElement = (Element) childPozycja;
listaKursow.append( childPozycjaElement.getTextContent() );
}
}
}
return listaKursow.toString();
}

Java: Accessing XML value as a new variable in another class

Java Programming
I have a class WeatherAgent. The function of this class is to get the current temperature value and other parameters from a URL (XML code) from World weather online.
My main question is; how to get the generated temperature value in class WeatherAgent into antother class (Boiler)? So, Accessing XML value in class WeatherAgent as a new variable in another class (class Boiler).
package assignment_4;
/*
* imports of class WeatherAgent2
*/
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
/*
*start class WeatherAgent2
*/
public class WeatherAgent {
private Document getDocument() {
Document doc = null;
try {
URL url = new URL(
"http://api.worldweatheronline.com/free/v1/weather.ashx?q=Eindhoven&format=xml&num_of_days=5&key=87xdd77n893f6akfs6x3jk9s");
URLConnection connection = url.openConnection(); // Connecting to
// URL specified
doc = parseXML(connection.getInputStream());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return doc;
}
private Document parseXML(InputStream stream) throws Exception
{
DocumentBuilderFactory objDocumentBuilderFactory = null;
DocumentBuilder objDocumentBuilder = null;
Document doc = null;
try
{
objDocumentBuilderFactory = DocumentBuilderFactory.newInstance();
objDocumentBuilder = objDocumentBuilderFactory.newDocumentBuilder();
doc = objDocumentBuilder.parse(stream);
}
catch (Exception ex)
{
throw ex;
}
return doc;
}
/*
* get the temperature
*/
public double temperature() {
Document doc = getDocument();
Node Temperature;
Double temp = 0.0;
NodeList forecast = doc.getElementsByTagName("temp_C");
for (int i = 0; i < forecast.getLength(); i++) {
Element element = (Element) forecast.item(i);
NodeList list1 = (NodeList) element.getChildNodes();
Temperature = list1.item(0);
temp = Double.parseDouble(Temperature.getNodeValue());
}
return temp;
}
/*
* get cloud cover
*/
public double cloudcover() {
Document doc = getDocument();
Node Cloudcover;
Double cloudcover = 0.0;
NodeList forecast = doc.getElementsByTagName("cloudcover");
for (int i = 0; i < forecast.getLength(); i++) {
Element element = (Element) forecast.item(i);
NodeList list2 = (NodeList) element.getChildNodes();
Cloudcover = list2.item(0);
cloudcover = Double.parseDouble(Cloudcover.getNodeValue());
}
return cloudcover;
}
/*
* print method
*/
public static void main(String[] argvs) {
WeatherAgent wp = new WeatherAgent();
System.out.println("Temperature: " + wp.temperature());
System.out.println("Cloudcover: " + wp.cloudcover());
}
}
I have this methode in class Boiler to get the temperature value from class WeatherAgent into class Boiler.
It is not working and I do know why. Thanks already for your help!
public double getValueFromAgent(){
double agentTemp = send(URL.create("http://api.worldweatheronline.com/free/v1/weather.ashx?q=Eindhoven&format=xml&num_of_days=5&key=87xdd77n893f6akfs6x3jk9s", "temperature" , null, Double.class));
return agentTemp;
}
There are design patterns for how to structure your classes to share information like this. I'd look into the Observer pattern and the Mediator pattern.
Or in main, when you get the temperature from the WeatherAgent, push it into Boiler. With all of these, the idea is to decouple Boiler from knowing anything about WeatherAgent. It helps with reusability, and makes testing them independently a lot easier.

java xml parsing dblp

this is the xml file
please how to parse the tag author example we dont know how many author for each inproceeding ?
<?xml version="1.0" encoding="ISO-8859-1"?>
<dblp>
<inproceedings mdate="2014-01-18" key="series/sci/AzzagL13">
<author>Hanane Azzag</author>
<author>Mustapha Lebbah</author>
<title>A New Way for Hierarchical and Topological Clustering.</title>
<pages>85-97</pages>
<year>2011</year>
<booktitle>EGC (best of volume)</booktitle>
<ee>http://dx.doi.org/10.1007/978-3-642-35855-5_5</ee>
<crossref>series/sci/2013-471</crossref>
<url>db/series/sci/sci471.html#AzzagL13</url>
</inproceedings>
<inproceedings mdate="2014-01-18" key="series/sci/RabatelBP13">
<author>Julien Rabatel</author>
<author>Sandra Bringay</author>
<author>Pascal Poncelet</author>
<title>Mining Sequential Patterns: A Context-Aware Approach.</title>
<pages>23-41</pages>
<year>2011</year>
<booktitle>EGC (best of volume)</booktitle>
<ee>http://dx.doi.org/10.1007/978-3-642-35855-5_2</ee>
<crossref>series/sci/2013-471</crossref>
<url>db/series/sci/sci471.html#RabatelBP13</url>
</inproceedings>
</dblp>
Use Xpath, it's fast and powerfull , these lines for your example return 5 lines
Code:
final Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new FileInputStream("input.xml"));
final XPath xPath = XPathFactory.newInstance().newXPath();
final NodeList nodeList = (NodeList) xPath.compile("//author").evaluate(document, XPathConstants.NODESET);
for (int i = 0; i < nodeList.getLength(); i++) {
System.out.println(nodeList.item(i).getFirstChild().getNodeValue());
}
Displays:
Hanane Azzag
Mustapha Lebbah
Julien Rabatel
Sandra Bringay
Pascal Poncelet
Following code parse using apache digester which is commonly used while parsing in real projects. Nice one from apache community
// Updated code as per you need.
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import org.apache.commons.digester.Digester;
import org.apache.commons.digester.Rule;
import org.apache.commons.digester.Rules;
import org.xml.sax.InputSource;
public class Parsing {
public static void main(String[] args) throws Exception{
InputStream data = new FileInputStream("E:\\workspace\\trunk\\Parsing\\src\\data.xml");
byte[] b = new byte[data.available()];
// data.read(b);
Digester digester = new Digester();
//Genearting Array list while encountering dblp xpath
digester.addObjectCreate("dblp", HashMap.class);
digester.addObjectCreate("dblp/inproceedings", ArrayList.class);
//Calling add method while encountering author xpath
AuthorRule rule = new AuthorRule();
digester.addRule("dblp/inproceedings/author", rule);
digester.addRule("dblp/inproceedings/title", rule);
digester.addRule("dblp/inproceedings", rule);
HashMap parsedData = (HashMap) digester.parse(data);
Iterator<Entry<String, ArrayList>> dataItr = parsedData.entrySet().iterator();
while(dataItr.hasNext()){
Entry<String, ArrayList> entry = dataItr.next();
System.out.println("Title : " + entry.getKey() + ", Authors" + entry.getValue().toString());
}
}
private static class AuthorRule extends Rule{
String currentTitle = "";
#Override
public void body(String namespace, String name, String text)
throws Exception {
HashMap object = (HashMap) digester.peek(1);
ArrayList authors = (ArrayList) digester.peek(0);
if(name.equals("title")){
currentTitle = text;
}
else if(name.equals("author")){
authors.add(text);
}
}
#Override
public void end(String namespace, String name) throws Exception {
HashMap object = (HashMap) digester.peek(1);
ArrayList authors = (ArrayList) digester.peek(0);
if(name.equals("inproceedings")){
object.put(currentTitle, authors);
}
}
}
}
output::
Title : A New Way for Hierarchical and Topological Clustering., Authros[Hanane Azzag, Mustapha Lebbah]
Title : Mining Sequential Patterns: A Context-Aware Approach., Authros[Julien Rabatel, Sandra Bringay, Pascal Poncelet]
there are number of ways, e.g. via DOM:
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.File;
public class XmlAuthorReader {
public static void main(String argv[]) {
try {
File fXmlFile = new File(<filePath>);
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
NodeList nList = doc.getElementsByTagName("author");
System.out.println(nList.getLength()+ " author(s) found");
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
System.out.println("Author: " + nNode.getTextContent());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
you can find more variants here: http://www.mkyong.com/tutorials/java-xml-tutorials/

Categories

Resources