<?xml version="1.0"?>
<events>
<event>
<id>1234567</id>
<Assets>
<Asset>
<UploadURL>
https://www.mkyong.com/java/how-to-construct-a-file-path-in-java/
</UploadURL>
<FTP>
<RequestDT>2016-02-29 12:36:52 -0500</RequestDT>
<ResponseDT>2016-02-29 12:36:58 -0500</ResponseDT>
</FTP>
<URL>
<Value>
http://www.mkyong.com/java/how-to-read-xml-file-in-java-dom-parser/
</Value>
</URL>
</Asset>
</Assets>
</event>
</events>
Java file:
package src;
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;
public class ReadXMLFile {
public static void main(String argv[]) {
try {
File fXmlFile = new File("/Users/mykong/Documents/staff.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
//optional, but recommended
//read this - http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work
doc.getDocumentElement().normalize();
System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
NodeList nList = doc.getElementsByTagName("staff");
System.out.println("----------------------------");
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
System.out.println("\nCurrent Element :" + nNode.getNodeName());
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
System.out.println("Staff id : " + eElement.getElementsByTagName("id").item(0).getTextContent());
System.out.println("First Name : " + eElement.getElementsByTagName("UploadURL").item(0).getTextContent());
// System.out.println("Last Name : " + eElement.getElementsByTagName("lastname").item(0).getTextContent());
// System.out.println("Nick Name : " + eElement.getElementsByTagName("nickname").item(0).getTextContent());
// System.out.println("Salary : " + eElement.getElementsByTagName("salary").item(0).getTextContent());
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
I am not getting any output for this. Am I doing something wrong?
Well, the xml file don't have this node.
doc.getElementsByTagName("staff");
Related
I have got an xml file and try to read in some information and try to arrange them.
The data in the xml looks like:
<Class code="1-10" kind="category">
<Meta name="P17b-d" value="2"/>
<SuperClass code="1-10...1-10"/>
<SubClass code="1-100"/>
<Rubric kind="preferred">
<Label xml:lang="de" xml:space="default">Klinische Untersuchung</Label>
</Rubric>
</Class>
and my Java class looks like:
import java.io.File;
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;
public class Importer {
public static void main(String[] args) {
try {
File inputFile = new File("ops2022.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(inputFile);
doc.getDocumentElement().normalize();
NodeList nList = doc.getElementsByTagName("Class");
for (int temp = 0; temp < 10; temp++) {
Node nNode = nList.item(temp);
System.out.println("\nCurrent Element :" + nNode.getNodeName() );
Element iElement = (Element) nNode;
if (nNode.getNodeType() == Node.ELEMENT_NODE && iElement.getAttribute("kind").equals("category") ) {
Element eElement = (Element) nNode;
System.out.println("code : "
+ eElement.getAttribute("code"));
System.out.println("Label : "
+ eElement
.getElementsByTagName("Label")
.item(0)
.getTextContent());
System.out.println("SuperClass : "
+ eElement
.getElementsByTagName("SuperClass")
//I don't know how to get the attribute code here
);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
But how do I get the attribute's information of the "SuperClass" Node? Idon't know why but java handles eElement.getAttributeNode("SuperClass") as a node, although it is an Element. So I can't use the getAttribute().
I added the code in your answer (#Hiran Chaudhuri) to get my needed information:
import java.io.File;
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;
public class Importer {
public static void main(String[] args) {
try {
File inputFile = new File("ops2022.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(inputFile);
doc.getDocumentElement().normalize();
NodeList nList = doc.getElementsByTagName("Class");
for (int temp = 0; temp < 10; temp++) {
Node nNode = nList.item(temp);
System.out.println("\nCurrent Element :" + nNode.getNodeName() );
Element iElement = (Element) nNode;
if (nNode.getNodeType() == Node.ELEMENT_NODE && iElement.getAttribute("kind").equals("category") ) {
Element eElement = (Element) nNode;
System.out.println("code : "
+ eElement.getAttribute("code"));
System.out.println("Label : "
+ eElement
.getElementsByTagName("Label")
.item(0)
.getTextContent());
System.out.println("SuperClass : "
+ eElement
.getElementsByTagName("SuperClass")
Node n = eElement.getElementsByTagName("SuperClass").item(0);
if (n instanceof Attr) {
Attr a = (Attr)n;
System.out.println(a.getName());
System.out.println(a.getValue());
}
);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
And I get the following
----------------------------
Current Element :Class
Current Element :Class
Current Element :Class
code : 1-10
Label : Klinische Untersuchung
and if I add another else clause like
else {
Attr a = (Attr)n;
System.out.println(a.getValue());
}
java throws the following error:
java.lang.ClassCastException: class com.sun.org.apache.xerces.internal.dom.DeferredElementImpl cannot be cast to class org.w3c.dom.Attr (com.sun.org.apache.xerces.internal.dom.DeferredElementImpl and org.w3c.dom.Attr are in module java.xml of loader 'bootstrap')
at Importer.main(Importer.java:46)
.
With Element.getAttributeNode() you do receive a subclass/subinterface of Node called Attr. This Attr has getName() and getValue() methods that you should be interested in.
Using Element.getAttribute() will directly deliver the value of the corresponding attribute.
If you lost the chance to directly obtain the correct type, you can still recover like
Node n = ... // this is the attribute you are interested in
if (n instanceof Attr) {
Attr a = (Attr)n;
System.out.println(a.getName());
System.out.println(a.getValue());
}
So you are wondering how to access the SuperClass' code attribute. This code prints exactly the one value:
Document doc = dBuilder.parse(inputFile);
NodeList nList = doc.getElementsByTagName("Class"); // this list only contains Element nodes
for (int temp = 0; temp < nList.getLength(); temp++) {
Element nNode = (Element)nList.item(temp); // this is one 'class' element
NodeList nList2 = nNode.getElementsByTagName("SuperClass"); // this list only contains Element nodes
for (int temp2 = 0; temp2 < nList2.getLength(); temp2++) {
Element superclass = (Element)nList2.item(temp2);
String code = superclass.getAttribute("code");
System.out.println(code);
}
}
However this code does the same:
Document doc = dBuilder.parse(inputFile);
XPath xpath = XPathFactory.newInstance().newXPath();
String code = xpath.evaluate("/Class/SuperClass/#code", doc);
With XPath expressions you can navigate the DOM tree much more efficiently.
The following code did the job for me:
for (int i = 0; i < nList.getLength(); i++) {
//for (int i = 0; i < 20; i++) {
Node nNode = nList.item(i);
//System.out.println("\nCurrent Element :" + nNode.getNodeName() );
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
String supString = "OPS-2022";
NodeList fieldNodes = eElement.getElementsByTagName("SuperClass");
for(int j = 0; j < fieldNodes.getLength(); j++) {
Node fieldNode = fieldNodes.item(j);
NamedNodeMap attributes = fieldNode.getAttributes();
Node attr = attributes.getNamedItem("code");
if(attr != null) {
supString =attr.getTextContent();
}
}
}
}
Thanks for your help!
I am reading specific information from an XML File. I am having issues trying to read some elements like the DataDate. I am getting a NullPointerException. I think it happens because in the XML file there are two nodes with the word "Project" and the first one does not have a DataDate.
I do not know how to fix this error.
This is a part of the XML File that I am reading:
package testReadXML;
import java.io.File;
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;
public class TestReadXML {
public static void main(String[] args) {
try {
File xmlFile = new File("C:/Users/diani/Downloads/XML Files/CS01.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(xmlFile);
doc.getDocumentElement().normalize();
System.out.println("Root element:" + doc.getDocumentElement().getNodeName());
NodeList nList = doc.getElementsByTagName("Project");
for (int i = 0; i < nList.getLength(); i ++) {
Node nNode = nList.item(i);
System.out.println("\n" + nNode.getNodeName());
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
System.out.println("Object Id : " + eElement.getAttribute("ObjectId"));
System.out.println("Id : " + eElement.getElementsByTagName("Id").item(0).getTextContent());
System.out.println("Name : " + eElement.getElementsByTagName("Name").item(0).getTextContent());
System.out.println("Data Date : " + eElement.getElementsByTagName("DataDate").item(0).getTextContent());
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Just add this If condition when you are fetching the elements
if(eElement.getElementsByTagName("DataDate").getLength() > 0) {
System.out.println("Object Id : " + eElement.getAttribute("ObjectId"));
System.out.println("Id : " + eElement.getElementsByTagName("Id").item(0).getTextContent());
System.out.println("Name : " + eElement.getElementsByTagName("Name").item(0).getTextContent());
System.out.println("Data Date : " + eElement.getElementsByTagName("DataDate").item(0).getTextContent());
}
I am trying to parse an XML file into java class. But it is giving me an Error saying
Could not find or load main class
The XML file which i am parsing is in same directory as the PretechDOMReader.java 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 PretechDOMReader {
public static void main(String[] args) {
try {
File fXmlFile = new File("PretechDOM.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory
.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
System.out.println("Order Details"
+ doc.getDocumentElement().getNodeName());
NodeList nList = doc.getElementsByTagName("Ordernumber");
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
Node orderedItem1 = (Node) (NodeList) eElement
.getElementsByTagName("OrderedItem1").item(0)
.getChildNodes().item(0);
System.out.println("OrderedItem1 :"
+ orderedItem1.getNodeValue());
Node orderitemPrice = (Node) (NodeList) eElement
.getElementsByTagName("OrderitemPrice").item(0)
.getChildNodes().item(0);
System.out.println("OrderitemPrice :"
+ orderitemPrice.getNodeValue());
Node orderitemTax = (Node) (NodeList) eElement
.getElementsByTagName("OrderitemTax").item(0)
.getChildNodes().item(0);
System.out.println("OrderitemTax :"
+ orderitemTax.getNodeValue());
Node orderDiscount = (Node) (NodeList) eElement
.getElementsByTagName("OrderDiscount").item(0)
.getChildNodes().item(0);
System.out.println("OrderDiscount :"
+ orderDiscount.getNodeValue());
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Here is my XML file
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Order>
<Ordernumber Number="9988">
<OrderedItem1>Pepsi</OrderedItem1>
<OrderitemPrice>10</OrderitemPrice>
<OrderitemTax>10</OrderitemTax>
<OrderDiscount>20</OrderDiscount>
</Ordernumber>
<Ordernumber Number="9999">
<OrderedItem1>Cocacola</OrderedItem1>
<OrderitemPrice>11</OrderitemPrice>
<OrderitemTax>11</OrderitemTax>
<OrderDiscount>22</OrderDiscount>
</Ordernumber>
Your code is working well.
Please check Class Path setting of your project in Eclipse.
If I copy and paste the xml from this site into a xml file I can parse it with java
http://api.indeed.com/ads/apisearch?publisher=8397709210207872&q=java&l=austin%2C+tx&sort&radius&st&jt&start&limit&fromage&filter&latlong=1&chnl&userip=1.2.3.4&v=2
However, I want to parse it directly from a webpage if possible!
Here's my current code:
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;
import java.io.File;
import java.io.IOException;
public class XMLParser {
public void readXML(String parse) {
File xml = new File(parse);
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder;
try {
dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(xml);
// System.out.println("Root element :"
// + doc.getDocumentElement().getNodeName());
NodeList nList = doc.getElementsByTagName("result");
System.out.println("----------------------------");
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
// System.out.println("\nCurrent Element :" +
nNode.getNodeName());
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
System.out.println("job title : "
+
eElement.getElementsByTagName("jobtitle").item(0)
.getTextContent());;
System.out.println("Company: "
+
eElement.getElementsByTagName("company")
.item(0).getTextContent());
System.out.println("City : "
+
eElement.getElementsByTagName("city").item(0)
.getTextContent());
System.out.println("State : "
+
eElement.getElementsByTagName("state").item(0)
.getTextContent());
System.out.println("Country : "
+
eElement.getElementsByTagName("country").item(0)
.getTextContent());
System.out.println("Date posted : "
+
eElement.getElementsByTagName("date").item(0)
.getTextContent());
System.out.println("Job summary : "
+
eElement.getElementsByTagName("snippet").item(0)
.getTextContent());
System.out.println("Latitude : "
+
eElement.getElementsByTagName("latitude").item(0).getTextContent());
System.out.println("longitude : "
+
eElement.getElementsByTagName("longitude").item(0).getTextContent());
}
}
} catch (ParserConfigurationException | SAXException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
new XMLParser().readXML("test.xml");
}
}
any help would be appreciated.
Give it the URI instead of the XML. It will download it for you.
Document doc = dBuilder.parse(uriString)
Please find the code snippet like this
String url = "http://api.indeed.com/ads/apisearch?publisher=8397709210207872&q=java&l=austin%2C+tx&sort&radius&st&jt&start&limit&fromage&filter&latlong=1&chnl&userip=1.2.3.4&v=2";
try
{
DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
DocumentBuilder b = f.newDocumentBuilder();
Document doc = b.parse(url);
}
you need to have the element/nodes you want in a for loop. So it can scan through xml file, and find the right node you searching for.
reads the xml file as a string, and creates a xml structure
builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = builder.parse(connection.getInputStream());
NodeList nodes = doc.getElementsByTagName("mode");
for (int i = 0; i < nodes.getLength(); i++)
Element element = (Element) nodes.item(i);
//Gets tag from XML and it´s content
NodeList nodeMode = element.getElementsByTagName("mode");
Element elemMode = (Element) nodeMode.item(0);
and after if you want to pick out a value and parse to an int or what you want you do like this:
int currentMode = Integer.parseInt(elemMode.getFirstChild().getTextContent());
That's how I parsed data directly from url http://www.nbp.pl/kursy/xml/+something
static class Kurs {
public float kurs_sprzedazy;
public float kurs_kupna;
}
private static DocumentBuilder dBuilder;
private static Kurs getData(String filename, String currency) throws Exception {
Document doc = dBuilder.parse("http://www.nbp.pl/kursy/xml/"+filename+".xml");
doc.getDocumentElement().normalize();
NodeList nList = doc.getElementsByTagName("pozycja");
for(int i = 0; i < nList.getLength(); i++) {
Element nNode = (Element)nList.item(i);
if(nNode.getElementsByTagName("kod_waluty").item(0).getTextContent().equals(currency)) {
Kurs kurs = new Kurs();
String data = nNode.getElementsByTagName("kurs_sprzedazy").item(0).getTextContent();
data = data.replace(',', '.');
kurs.kurs_sprzedazy = Float.parseFloat(data);
data = nNode.getElementsByTagName("kurs_kupna").item(0).getTextContent();
data = data.replace(',', '.');
kurs.kurs_kupna = Float.parseFloat(data);
return kurs;
}
}
return null;
}
I need to receive all the text alone from an xml file for receiving the specific tag i use this code. But i am not sure how to parse all the text from the XML i the XML files are different i don't know their root node and child nodes but i need the text alone from the xml.
try {
DocumentBuilderFactory dbFactory = DocumentBuilderFactory
.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(streamLimiter.getFile());
doc.getDocumentElement().normalize();
System.out.println("Root element :"
+ doc.getDocumentElement().getNodeName());
NodeList nList = doc.getElementsByTagName("employee");
System.out.println("-----------------------");
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
NodeList nlList = eElement.getElementsByTagName("firstname")
.item(0).getChildNodes();
Node nValue = (Node) nlList.item(0);
System.out.println("First Name : "
+ nValue.getNodeValue());
}
}
} catch (Exception e) {
e.printStackTrace();
}
Quoting jsight's reply in this post: Getting XML Node text value with Java DOM
import java.io.ByteArrayInputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
class Test {
/**
* #param args the command line arguments
*/
public static void main(String[] args) throws Exception {
String xml = "<add job=\"351\">\n"
+ " <tag>foobar</tag>\n"
+ " <tag>foobar2</tag>\n"
+ "</add>";
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
ByteArrayInputStream bis = new ByteArrayInputStream(xml.getBytes());
org.w3c.dom.Document doc = db.parse(bis);
Node n = doc.getFirstChild();
NodeList nl = n.getChildNodes();
Node an, an2;
for (int i = 0; i < nl.getLength(); i++) {
an = nl.item(i);
if (an.getNodeType() == Node.ELEMENT_NODE) {
NodeList nl2 = an.getChildNodes();
for (int i2 = 0; i2 < nl2.getLength(); i2++) {
an2 = nl2.item(i2);
// DEBUG PRINTS
System.out.println(an2.getNodeName() + ": type (" + an2.getNodeType() + "):");
if (an2.hasChildNodes()) {
System.out.println(an2.getFirstChild().getTextContent());
}
if (an2.hasChildNodes()) {
System.out.println(an2.getFirstChild().getNodeValue());
}
System.out.println(an2.getTextContent());
System.out.println(an2.getNodeValue());
}
}
}
}
}
Output:
#text: type (3):
foobar
foobar
#text: type (3):
foobar2
Adapt this code to your problem and it should work.