This is the code I have at present:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.*;
import java.io.File;
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 XMLReader extends JFrame
{
JFileChooser _fileChooser = new JFileChooser();
JPanel content = new JPanel();
//... Create menu elements (menubar, menu, menu item)
JMenuBar menubar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
JMenuItem openItem = new JMenuItem("Open...");
int retval = _fileChooser.showOpenDialog(XMLReader.this);
//... The user selected a file, get it, use it.
public static void main(String argv[])
{
ArrayList timeStamp = new ArrayList();
ArrayList Y = new ArrayList();
File file = XMLReader.this;
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 nodeLst = doc.getElementsByTagName("reading");
System.out.println("Share Data");
for (int s = 0; s < nodeLst.getLength(); s++) {
Node fstNode = nodeLst.item(s);
if (fstNode.getNodeType() == Node.ELEMENT_NODE) {
Element fstElmnt = (Element) fstNode;
NodeList fstNmElmntLst = fstElmnt.getElementsByTagName("timeStamp");
.
.
.
}
I am trying to get a dialog box to allow the user to pick the XML they want parsed. I know the parsing works as I had hard coded in the file before.
I would also like to return the ArrayLists so that I can use them as the inputs to another class is this possible (at the moment I am only printing them to screen)?
System.out.println(timeStamp);
System.out.println(Y);
Can I use a return statement and if so how to I set up the class that I want to use them in?
The dialog box serve to retrive the path
I see that You are using the awt and dom so:
public Document loadXmlFile(Frame frame, DocumentBuilder docBuilder, String startPath) {
FileDialog fd = new FileDialog(frame, "Loadxml-title", FileDialog.LOAD);
//Add type filter
fd.setDirectory(startPath));
fd.show();
String file = fd.getFile();
if(file == null) {
return null;
}
return docBuilder.parse(file);
}
Q2: "Error message when compiling, can't have DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); with throw or catch exception."
This error is raised bu compiler because You don't have the block try & catch in method main.
The definition of DocumentBuilderFactory.newInstace(); says that this method throw the FactoryConfigurationError. In Java when method throw some error and it is not a RuntimeException you need to throw it in method definition or add the try catch block to handle it.
DocumentBuilderFactory dbf = null;
try {
dbf = DocumentBuilderFactory.newInstance();
} Exception(FactoryConfigurationError e){
e.printStackTrace();
}
Related
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);
}
}
}
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/
I need to be able to pull out the USD conversion from Euro using this xml:
http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml
I've gotten it to work if I manually download the xml and parse to the data I need from there, but I'm not sure how to grab it directly from the url.
Thank you
If i understand well, you are having trouble obtaining the xml content directly from the URL, is that right?
If so, this might help you.
// the SAX way:
XMLReader myReader = XMLReaderFactory.createXMLReader();
myReader.setContentHandler(handler);
myReader.parse(new InputSource(new URL(url).openStream()));
// or if you prefer DOM:
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new URL(url).openStream());
It applies well to a service or stand alone context, but if you are trying to access the XML from your WEB tier you might consider an AJAX approach.
Cant make any easier than that:
package com.danielmarreco;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Element;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.InputStream;
import java.net.URL;
public class Main {
public static void main(String[] args) {
try {
InputStream is = new URL("http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml").openStream();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(is);
NodeList nodeList = doc.getElementsByTagName("Cube");
for (int i = 0; i < nodeList.getLength(); i ++) {
Node node = nodeList.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element) node;
if(element.getAttribute("currency").equals("BRL"))
System.out.println("1 EUR = " + element.getAttribute("rate") + " BRL");
}
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
I have a folder with some stock price files in the form stockA.txt
I am trying to create a java program that stores the filename without the extension in an xml file as a node.
For example the file.xml should be in the form :
<stockA></stockA>
<stockB></stockB>
The problem is that I am getting the following error code :
Exception in thread "main" org.w3c.dom.DOMException: HIERARCHY_REQUEST_ERR: An attempt was made to insert a node where it is not permitted.
Here is the code
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package xmlfilecreation;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
/**
*
* #author Administrator
*/
public class XMLFileCreation {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
File folder = new File("/Users/Administrator/Documents/TM470/NYSE_EOD/NyseLast2Years/");
File[] listOfFiles = folder.listFiles();
List<String> results = new ArrayList<String>();
for (int i = 0; i < listOfFiles.length; i++)
{
results.add(listOfFiles[i].getName().replace(".txt", ""));
//System.out.println(results.get(i));
}
try
{
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
//elements
Document doc = docBuilder.newDocument();
//root element
Element rootElement = doc.createElement("Stocks");
doc.appendChild(rootElement);
int counter = 0;
Element myElement;
for (String item: results)
{
myElement = doc.createElement(item);
doc.appendChild(myElement);
rootElement.appendChild(myElement);
counter++;
}
System.out.println(counter);
//write the content into xml file
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File("file.xml"));
transformer.transform(source, result);
System.out.println("File saved!");
}
catch (ParserConfigurationException ex)
{
ex.printStackTrace();
}
catch(TransformerException ex)
{
ex.printStackTrace();
}
}
}
How can I fix that error?
Thank you.
You're appending twice the elements, one time at root level, and the other one in the Stocks tag. You just need to do it only once.
for (String item: results) {
Element myElement = doc.createElement(item);
// doc.appendChild(myElement); -> You don't need this one
rootElement.appendChild(myElement);
counter++;
}
Appending to the root element does suffice.
I have the following simplified XML:
<?xml version="1.0" encoding="UTF-8"?>
<ExportData>
<Rows>
<R>
<companyCodestringtrue>101</companyCodestringtrue>
<transactionQualifierstring>Sales</transactionQualifierstring>
<menuItemNumberlong>4302150</menuItemNumberlong>
<productQuantityinttrue>14</productQuantityinttrue>
<productValueInclVATdecimaltrue>1.90</productValueInclVATdecimaltrue>
<productValueExclVATdecimaltrue>1.775701</productValueExclVATdecimaltrue>
</R>
<R>
<companyCodestringtrue>101</companyCodestringtrue>
<transactionQualifierstring>Sales</transactionQualifierstring>
<menuItemNumberlong>333555</menuItemNumberlong>
<productQuantityinttrue>0</productQuantityinttrue>
<productValueInclVATdecimaltrue>3.90</productValueInclVATdecimaltrue>
<productValueExclVATdecimaltrue>3.775701</productValueExclVATdecimaltrue>
</R>
<R>
<companyCodestringtrue>101</companyCodestringtrue>
<transactionQualifierstring>Sales</transactionQualifierstring>
<menuItemNumberlong>1235665</menuItemNumberlong>
<productQuantityinttrue>5</productQuantityinttrue>
<productValueInclVATdecimaltrue>4.90</productValueInclVATdecimaltrue>
<productValueExclVATdecimaltrue>4.775701</productValueExclVATdecimaltrue>
</R>
</Rows>
</ExportData>
I need to delete each complete <R> element if the <productQuantityinttrue> element equals "0".
I came up with the following Java code:
package filterPositions;
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class FilterPositions {
public static String result = "";
public static void main(String[] args) throws Exception {
try {
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
File filePath = new File("C:/LSA_SALES_EXPORT_1507_test_zero_qu.xml");
Document doc = docBuilder.parse(filePath);
Node rootNode = doc.getDocumentElement();
final Element element = doc.getDocumentElement();
// output new XML Document
DocumentBuilder parser = docFactory.newDocumentBuilder();
Document newdoc = parser.newDocument();
newdoc.adoptNode(traversingXML(element));
writeXmlFile(newdoc, "LSA_SALES_EXPORT_1507_test_zero_qu_OUT.xml");
System.out.println("Done...");
System.out.println("Exiting...");
} catch (Exception e) {
e.printStackTrace();
}
}
public static Element traversingXML(Element element) {
NodeList positionen = element.getElementsByTagName("R");
Element e = null;
for (int i = 0; i < positionen.getLength(); i++) {
e = (Element) positionen.item(i);
for (Node child = e.getFirstChild(); child != null; child = child.getNextSibling()) {
if (child instanceof Element && "productQuantityinttrue".equals(child.getNodeName())&& "0".equals(child.getTextContent())) {
e.getParentNode().removeChild(e);
}
}
}
System.out.println(e);
return e;
}
public static void writeXmlFile(Document doc, String filename) {
try {
// Prepare the DOM document for writing
Source source = new DOMSource();
// Prepare the output file
File file = new File(filename);
Result result = new StreamResult(file);
// Write the DOM document to the file
Transformer xformer = TransformerFactory.newInstance()
.newTransformer();
xformer.transform(source, result);
} catch (TransformerConfigurationException e) {
} catch (TransformerException e) {
}
}
}
I am not sure if my method "traversingXML" is working properly. My problem right now is that the adapted XML structure (one deleted) is not written to newdoc.
You don't copy the original document to newdoc; instead you create a new, empty XML document.
Instead, try this code:
...
final Element element = doc.getDocumentElement(); // original code up to here
traversingXML(element); // delete the node
writeXmlFile(doc, "LSA_SALES_EXPORT_1507_test_zero_qu_OUT.xml"); // save modified document