Reputation: 2080
How can i extract attribute value out of the element. My xml node is writen like this < nodename attribute="value" > i need to extract it out to compare it against another string.
But since i am not calling document.getElementsByTag then i cant use .getAttribute("att.").getNodeValue to get the value.
Instead i have a NodeList and getAttribute() does not have getNodeValue.
package dev;
import java.io.*;
import java.util.*;
import javax.xml.parsers.*;
import javax.xml.xpath.*;
import org.w3c.dom.*;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
public class Parser {
static String def = "\"admin\",\"base\",\"Default\",\"simple\"";
static String category = "";
static String sku = "";
static String has_options = "0";
static String name = "";
static String image = "";
static String small_image = "";
static String thumbnail = "";
public static void toCSV() {
try {
BufferedWriter output = new BufferedWriter(new FileWriter("sim.csv", true));
output.newLine();
output.write(def);
output.write(String.format(",\"%s\",\"%s\",\"%s\"", category, sku, has_options));
output.write(String.format(",\"%s\",\"%s\",\"%s\",\"%s\"", name, image, small_image, thumbnail));
output.flush();
output.close();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
toCSV();
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(new File("input.asp.xml"));
document.getDocumentElement().normalize();
NodeList list = document.getElementsByTagName("izdelek");
for(int i = 0; i < 1; i++) {
NodeList child = list.item(i).getChildNodes();
for(int j = 0; j < child.getLength(); j++) {
if(child.item(j).getNodeName().equals("kategorija")) {
category = child.item(j).getTextContent().trim();
} else if(child.item(j).getNodeName().equals("ean")) {
sku = child.item(j).getTextContent().trim();
} else if(child.item(j).getNodeName().equals("izdelekIme")) {
name = child.item(j).getTextContent().trim();
} else if(child.item(j).getNodeName().equals("slikaMala")) {
small_image = child.item(j).getTextContent().trim();
thumbnail = child.item(j).getTextContent().trim();
} else if(child.item(j).getNodeName().equals("slikaVelika")) {
image = child.item(j).getTextContent().trim();
} else if(child.item(j).getNodeName().equals("dodatneLastnosti")) {
NodeList subs = child.item(j).getChildNodes();
// ^ need to parse these nodes they are written as <nodename attribute="value">
// i need to print out the value
}
}
//toCSV();
}
} catch(Exception io) {
io.printStackTrace();
}
}
}
Upvotes: 3
Views: 23071
Reputation: 11
You also can use this,
child.item(j).getFirstChild().getNodeValue();
Upvotes: 0
Reputation: 2080
Solved:
XML input:
< nodename attribute="value" > Something </ nodename>
Java code:
NodesList subs = child.item(j).getChildNodes();
System.out.println(subs.item(0).getTextContent()); // >> Something
Element element = (Element) document.adoptNode(subs.item(0));
System.out.println(element.getAttribute("attribute")); // >> value
Upvotes: 5