Parsing XML with attributes in Java? - java

I have a url: http://ws.audioscrobbler.com/2.0/?method=chart.getlovedtracks&api_key=b25b959554ed76058ac220b7b2e0a026&page=3&limit=25 that display XML with information on A song name and artist. It also display 3 links to different sized images, these image urls are identified by
<image size="small">
<image size="large">
<image size="extralarge">
Here is the code I am using at the moment however I do not know how to get the url for the medium sized image only ???
private void parseXml(String urlPath) throws Exception {
URL url = new URL(urlPath);
URLConnection connection = url.openConnection();
DocumentBuilder db = DOCUMENT_BUILDER_FACTORY.newDocumentBuilder();
final Document document = db.parse(connection.getInputStream());
XPath xPathEvaluator = XPATH_FACTORY.newXPath();
XPathExpression nameExpr = xPathEvaluator.compile("lfm/tracks/track/name");
NodeList trackNameNodes = (NodeList) nameExpr.evaluate(document, XPathConstants.NODESET);
for (int i = 0; i < trackNameNodes.getLength(); i++) {
Node trackNameNode = trackNameNodes.item(i);
System.out.println(String.format("Track Name: %s" , trackNameNode.getTextContent()));
XPathExpression artistImageExpr = xPathEvaluator.compile("following-sibling::/image");
NodeList artistImageNodes = (NodeList) artistImageExpr.evaluate(trackNameNode, XPathConstants.NODESET);
for (int j=0; j < artistImageNodes.getLength(); j++) {
System.out.println(String.format(" -Image URL: %s", artistImageNodes.item(j).getTextContent()));
}
XPathExpression artistNameExpr = xPathEvaluator.compile("following-sibling::artist/name");
NodeList artistNameNodes = (NodeList) artistNameExpr.evaluate(trackNameNode, XPathConstants.NODESET);
for (int j=0; j < artistNameNodes.getLength(); j++) {
System.out.println(String.format(" - Artist Name: %s", artistNameNodes.item(j).getTextContent()));
}
}
}

Related

How do I populate the XML attributes to an String (Preferably an Observable List)

Here is the XML code:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<CATALOG>
<FLOWCHART id="FC1">
<PRIMARYCODE>FC1</PRIMARYCODE>
<NAME>Flowchart 1</NAME>
<STEPS>
<STEP id="1">was powered on.</STEP>
<STEP id="2">was not connected with a connection plate.</STEP>
</STEPS>
</FLOWCHART>
<FLOWCHART id = "FC2">
<PRIMARYCODE>FC2</PRIMARYCODE>
<NAME>Flowchart2</NAME>
<STEPS>
<STEP id="1">was not powered on.</STEP>
<STEP id="2">was connected with a connection plate.</STEP>
<STEP id="3">Driver was not installed.</STEP>
<STEP id="4">Software was installed.</STEP>
</STEPS>
</FLOWCHART>
</CATALOG>
Here is the method that I have created to try and populate the id attribute of the flowchart. I am essentially trying to populate these options in a Choicebox.
public static String[] flowChartList(Document doc) throws XPathExpressionException {
XPathFactory xpf = XPathFactory.newInstance();
XPathExpression xpath = xpf.newXPath().compile("/CATALOG/FLOWCHART");
NodeList nodeList = (NodeList) xpath.evaluate(doc, XPathConstants.NODE);
String[] flowcharts = new String[nodeList.getLength()];
for (int index = 0; index < nodeList.getLength(); index++) {
Node nNode = nodeList.item(index);
Element eElement = (Element) nNode;
flowcharts[index] = eElement.getAttribute("id");
System.out.println("Found flowchart "+ flowcharts[index]);
}
return flowcharts;
}
Here is the version, which return list of IDs:
public static List<String> flowChartList(Document doc) throws Exception {
XPathFactory xpf = XPathFactory.newInstance();
XPathExpression xpath = xpf.newXPath().compile("/CATALOG/FLOWCHART");
NodeList nodeList = (NodeList) xpath.evaluate(doc, XPathConstants.NODESET);
List<String> flowcharts = new ArrayList<>();
for (int index = 0; index < nodeList.getLength(); index++) {
Node nNode = nodeList.item(index);
Element eElement = (Element) nNode;
flowcharts.add(eElement.getAttribute("id"));
}
return flowcharts;
}
Please note usage of XPathConstants.NODESET instead of XPathConstants.NODE.
Slightly modified version getting id attributes directly in XPath:
public static List<String> flowChartList(Document doc) throws Exception {
XPathFactory xpf = XPathFactory.newInstance();
XPathExpression xpath = xpf.newXPath().compile("/CATALOG/FLOWCHART/#id");
List<String> result = new ArrayList<>();
NodeList ns = (NodeList)xpath.evaluate(doc, XPathConstants.NODESET);
for(int i = 0; i < ns.getLength(); i++ ){
result.add(ns.item(i).getTextContent());
}
return result;
}

Maintaining count of XML elements using Xpath

I am a beginner in using Java programming particularly using Xpath to parse an XML file.
I am trying to develop a system that routes flights according to their weightings. I want to:
Maintain a count of flights for each location;
The system should accept a location and return the number and country to route a flight to. For every 4 flights to Japan, route the next 2 flights to China then route the next 2 flights to India and loop and to return count, continent, location name, country and weight.
I would appreciate any assistance please.
I can pass the XML data of and retrieve the different element nodes using Xpath. I attempted using SAX and STAX but prefered this method as it was clear and concise when constructing the expressions.
XML File Example:
<continent>
<location name = "asia">
<country>Japan</country>
<code>0000011111</code>
<weight>10</weight>
</location>
<location name = "asia">
<country>China</country>
<code>0000022222</code>
<weight>1</weight>
</location>
</continent>
Java Sample Code:
public static void main(String[] args) {
try {
FileInputStream file = new FileInputStream(new File("c:/continents.xml"));
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = builderFactory.newDocumentBuilder();
Document xmlDocument = builder.parse(file);
XPath xPath = XPathFactory.newInstance().newXPath();
System.out.println("*************************");
String expression = "/continent/location";
System.out.println(expression);
String name = xPath.compile(expression).evaluate(xmlDocument);
System.out.println(name);
System.out.println("**********Parse XML File***************");
expression = "/continent/location/country|//number|//weight";
System.out.println(expression);
NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODESET);
for (int i = 0; i < nodeList.getLength(); i++) {
System.out.println(nodeList.item(i).getFirstChild().getNodeValue());
}
System.out.println("*************************");
expression = "/continent/location[#name='asia']/number";
System.out.println(expression);
nodeList = (NodeList) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODESET);
for (int i = 0; i < nodeList.getLength(); i++) {
System.out.println(nodeList.item(i).getFirstChild().getNodeValue());
}
System.out.println("*************************");
expression = "//location[country='China']";
System.out.println(expression);
Node node = (Node) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODE);
if(null != node) {
nodeList = node.getChildNodes();
for (int i = 0;null!=nodeList && i < nodeList.getLength(); i++) {
Node nod = nodeList.item(i);
if(nod.getNodeType() == Node.ELEMENT_NODE)
System.out.println(nodeList.item(i).getNodeName() + " : " + nod.getFirstChild().getNodeValue());
}
}
}
I’m not sure I understand your goal, but if you just want to count how many flights are to a particular country, you can count the matching elements:
NodeList matches = (NodeList) xPath.evaluate(
"//country[text()='" + country + "']",
xmlDocument,
XPathConstants.NODESET);
int matchCount = matches.getLength();

Not able to parse duplicate xml tag values using DocumentBuilder in Java

I'm able to parse the XML object if it has a single unique inner tag. But the problem comes when I have two duplicate tags in a parent tag. How can I get both tag values? I'm getting the response as XML string.
Here is my code
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(responseXML));
if (is != null) {
Document doc = db.parse(is);
String errorCode = "";
NodeList errorDetails = doc.getElementsByTagName("ERROR-LIST");
if (errorDetails != null) {
int length = errorDetails.getLength();
if (length > 0) {
for (int i = 0; i < length; i++) {
if (errorDetails.item(i).getNodeType() == Node.ELEMENT_NODE) {
Element el = (Element) errorDetails.item(i);
if (el.getNodeName().contains("ERROR-LIST")) {
NodeList errorCodes = el.getElementsByTagName("ERROR-CODE");
for (int j = 0; j < errorCodes.getLength(); j++) {
Node errorCode1 = errorCodes.item(j);
logger.info(errorCode1.getNodeValue());
}
}
}
}
} else {
isValidResponse = true;
}
}
}
The response which I'm getting from server is
<DATA><HEADER><RESPONSE-TYPE CODE = "0" DESCRIPTION = "Response Error" />
</HEADER><BODY><ERROR-LIST>
<ERROR-CODE>9000</ERROR-CODE>
<ERROR-CODE>1076</ERROR-CODE>
</ERROR-LIST></BODY></DATA>
Im able to get only 9000 error code, how can I catch all error codes which are under error list?
Any ideas would be greatly appreciated.
You are explicitly requesting the first element of the error list:
el.getElementsByTagName("ERROR-CODE").item(0).getTextContent();
Loop over all the nodes getElementsByTagName returns.
NodeList errorCodes = el.getElementsByTagName("ERROR-CODE");
for (int j = 0; j < errorCodes.getLength(); j++) {
String errorCode = errorCodes.item(j).getTextContent();
}

XML parsing to Java - getting root attribute value

I'm having a slight problem with XML parsing.
I'm creating a function where the parameter is a certain "element" from the XML file.
When found, I want to return the value of the root attribute.
Here's my code:
FileInputStream file = new FileInputStream(new File("C:\\Users\\Grizzly\\Java\\Projet_16_17-20161214\\bdd.xml"));
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(file);
doc.getDocumentElement().normalize();
NodeList nList = doc.getElementsByTagName("type");
for (int temp = 0; temp < nList.getLength(); temp++)
{
Node nNode = nList.item(temp);
if(nNode.toString().equalsIgnoreCase(element))
{
Element eElement = (Element) nNode;
System.out.println("Taxe= "+ eElement.getAttribute("taxe"));
}
}
}
Any idea on how to do this?
Here's my XML file:
<?xml version="1.0"?>
-<types>
-<type id="Nourriture" taxe="0.1">
<element>pomme</element>
<element>fraise</element>
<element>fromage</element>
<element>viande rouge </element>
</type>
-<type id="Matiere Premiere" taxe="0.2">
<element>fer</element>
<element>polypropylene</element>
</type>
-<type id="Element Solide" taxe="0.3">
<element>voiture</element>
<element>planche surf</element>
<element>pistolet</element>
</type>
</types>
In my code, I tried to get the elements of a certain node from the nodelist and then compare it to the the string "element" which is the input of the user, and if they match it will check the attribute value of taxe linked to it.
Thanks in advance.
EDIT: I'm getting closer to what I need:
NodeList nList = doc.getElementsByTagName("type");
for (int temp = 0; temp < nList.getLength(); temp++)
{
Node nNode = nList.item(temp);
NodeList nChildren = nNode.getChildNodes();
Element eElement = (Element) nNode;
for(int i = 0; i < nChildren.getLength(); i++)
{
String onElement = eElement.getElementsByTagName("element").item(i).getTextContent();
if(onElement.equalsIgnoreCase(element))
{
System.out.println("id : " + eElement.getAttribute("id"));
System.out.println("taxe : " + eElement.getAttribute("taxe"));
break;
}
}
}
But it's only reading the first element... and item(i) isn't working.
Any idea?
If I understand you correctly, you are trying to fetch specific attributes (id and taxe) of all the document nodes having at least one child element with specific name (element).
Although the problem can be solved by iterating the DOM and keeping the states, I would rather delegate this task to XPath. A code with XPath will look cleaner and be more maintainable. For example, in order to fetch all elements having attributes id and taxe and a child element element you can use an XPath expression like //*[#id and #taxe element]. The matching nodes are fetched in a single line. You can simply iterate the nodes and collect the attributes as shown in the following example.
Example
public static void main(String args[]) {
String element = args.length > 0 ? args[0] : "element";
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder builder = factory.newDocumentBuilder();
FileInputStream file = new FileInputStream(new File("/some/file.xml"));
Document doc = builder.parse(file);
XPath xPath = XPathFactory.newInstance().newXPath();
String expression = "//*[#id and #taxe and " + element + "]";
NodeList nodeList = (NodeList) xPath.compile(expression)
.evaluate(doc, XPathConstants.NODESET);
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
NamedNodeMap attributes = node.getAttributes();
for (int j = 0; j < attributes.getLength(); j++) {
Node aNode = attributes.item(j);
System.out.printf(
"%s: %s\n",
aNode.getNodeName(),
aNode.getNodeValue()
);
}
}
} catch (Exception e) {
System.err.println(e.getMessage());
System.exit(1);
}
}
Sample Output
id: Nourriture
taxe: 0.1
id: Matiere Premiere
taxe: 0.2
id: Element Solide
taxe: 0.3
Note, the sample above prints all attributes of the parent element. If you want to print only specific ones, you can, obviously, add a trivial check like this:
String aName = aNode.getNodeName();
if (aName.equals("taxe")) { // ...
But you can actually filter out the attributes with XPath:
String expression = "//*[ " + element + "]/#*[name() = 'id' or name() = 'taxe']";
NodeList nodeList = (NodeList) xPath.compile(expression)
.evaluate(doc, XPathConstants.NODESET);
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
System.out.printf("%s: %s\n", node.getNodeName(), node.getNodeValue());
}
The XPath expression above fetches all attribute nodes having names equal to whether id, or taxe. If you want all attributes, simply remove the last condition:
String expression = "//*[ " + element + "]/#*";

XML parsing in java with xpath

I am trying to print out some data
so my code is
private static final DocumentBuilderFactory DOCUMENT_BUILDER_FACTORY = DocumentBuilderFactory.newInstance();
private static final XPathFactory XPATH_FACTORY = XPathFactory.newInstance();
public void parseXml(String urlPath) throws Exception {
URL url = new URL(urlPath);
URLConnection connection = url.openConnection();
DocumentBuilder db = DOCUMENT_BUILDER_FACTORY.newDocumentBuilder();
final Document document = db.parse(connection.getInputStream());
XPath xPathEvaluator = XPATH_FACTORY.newXPath();
XPathExpression nameExpr = xPathEvaluator.compile("lfm/results/trackmatches/track");
NodeList tracksinfoNodes = (NodeList) nameExpr.evaluate(document, XPathConstants.NODESET);
for (int i = 0; i < tracksinfoNodes.getLength(); i++) {
Node trackNameNode = tracksinfoNodes.item(i);
System.out.println(String.format("Track Name: %s" , trackNameNode.getTextContent()));
}
}
so it will print me like this for the first loop
Track Name:
I Believe in You
Neil Young
http://www.last.fm/music/Neil+Young/_/I+Believe+in+You
0
90540
http://userserve-ak.last.fm/serve/34s/65285990.png
http://userserve-ak.last.fm/serve/64s/65285990.png
http://userserve-ak.last.fm/serve/126/65285990.png
http://userserve-ak.last.fm/serve/300x300/65285990.png
the url I am using is http://ws.audioscrobbler.com/2.0/?method=track.search&track=Believe&api_key=b25b959554ed76058ac220b7b2e0a026
so what I am trying to do is to get eveyone alone , like this
song : I Believe in You
artist :Neil Young
link : http://www.last.fm/music/Neil+Young/_/I+Believe+in+You
something: 0
views: 90540
linkimg : http://userserve-ak.last.fm/serve/34s/65285990.png
..
..
Try something like this:
private static final DocumentBuilderFactory DOCUMENT_BUILDER_FACTORY = DocumentBuilderFactory.newInstance();
private static final XPathFactory XPATH_FACTORY = XPathFactory.newInstance();
public static void parseXml(String urlPath) throws Exception {
URL url = new URL(urlPath);
URLConnection connection = url.openConnection();
DocumentBuilder db = DOCUMENT_BUILDER_FACTORY.newDocumentBuilder();
final Document document = db.parse(connection.getInputStream());
XPath xPathEvaluator = XPATH_FACTORY.newXPath();
NodeList tracksinfoNodes = (NodeList) xPathEvaluator.compile("lfm/results/trackmatches/track").evaluate(
document, XPathConstants.NODESET);
for (int i = 0; i < tracksinfoNodes.getLength(); i++) {
Node trackNameNode = tracksinfoNodes.item(i);
NodeList childs = trackNameNode.getChildNodes();
for (int j = 0; j < childs.getLength(); j++) {
Node n = childs.item(j);
if (!n.getNodeName().equals("#text")) {
System.out.println(String.format("%s: %s", n.getNodeName(), n.getTextContent()));
}
}
System.out.println("==============");
}
}

Categories