Getting attribute from XML document - java

I've looked on here for solutions, but I am still having problems getting this attribute from my xml document. I am trying to fetch the "1" from this: <update-comments total="1">
Here the code that I am using to fetch the other values without attributes:
DocumentBuilder dbBuilder = dbFactory.newDocumentBuilder();
doc = dbBuilder.parse(stream);
doc.getDocumentElement().normalize();
NodeList nodes = doc.getElementsByTagName("update");
for (int i = 0; i < nodes.getLength(); i++) {
Node node = nodes.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element) node;
String update_type = getValue("update-type", element);
String numLikes = null;
String submittedUrl = null;
String comments = null;
if (update_type.equals("SHAR")) {
String shar_user = null;
String timestamp = null;
String id = null;
String updateKey = null;
String numComments = null;
try {
shar_user = getValue("first-name", element)
+ " " + getValue("last-name", element);
timestamp = getValue("timestamp", element);
id = getValue("id", element);
updateKey = getValue("update-key", element);
profilePictureUrl = getValue("picture-url", element);
numLikes = getValue("num-likes", element);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
}
private static String getValue(String tag, Element element)
{
NodeList nodes = element.getElementsByTagName(tag).item(0)
.getChildNodes();
Node node = (Node) nodes.item(0);
return node.getNodeValue();
}

This function will get an attribute value from an element using the same strategy that you used to find an element. (Note, you solution only works if an element actually exists.)
private static String getAttributeValue(String tag, Element element, String attribute)
{
NodeList nodes = element.getElementsByTagName(tag);
//note: you should actually check the list size before asking for item(0)
//because you asked for ElementsByTagName(), you can assume that the node is an Element
Element elem = (Element) nodes.item(0);
return elem.getAttribute(attribute);
}

Related

Java XML removeChild not working?

Hello so i've looked this question up alot but I couldn't find a solution that worked. I'm basically trying to remove the "job" node as seen declared in line 7 and removed in line 13. There's 0 runtime errors but the node doesn't get removed.
NodeList rootNodes = xml.getElementsByTagName("jobs");
Node rootNode = rootNodes.item(0);
Element rootElement = (Element) rootNode;
NodeList jobsList = rootElement.getElementsByTagName("job");
for (int i = 0; i < jobsList.getLength(); i++) {
Node job = jobsList.item(i);
Element jobElement = (Element) job;
if(jobElement.getAttribute("id").equals(
msgEvent.getMessage().getContentRaw().split(" ")[2]))
{
rootNode.removeChild(job);
msgEvent.getChannel().sendMessage("Removed Job " + jobElement.getAttribute("id") + " (Summary: '" + jobElement.getAttribute("summary") + "')").complete();
}
}
Here's the XML
<?xml version = "1.0"?>
<jobs>
<job payment = "50000" poster="171048434529337344" collect = "asdf" id = "1" summary="asdfd" expires="5/10/18"> </job>
<job payment = "10000" poster="171048434529337344" collect = "asdf" id = "2" summary="asdf" expires="5/10/18"> </job>
</jobs>
since this is too large for comment, Here is my test code and results:
public static void main(String[] args) {
try (InputStream is = Files.newInputStream(Paths.get("C://Temp/xx.xml"))) {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document xml = builder.parse(new InputSource(is));
NodeList rootNodes = xml.getElementsByTagName("jobs");
Node rootNode = rootNodes.item(0);
Element rootElement = (Element) rootNode;
NodeList jobsList = rootElement.getElementsByTagName("job");
System.out.println("list before removal");
for (int i = 0; i < jobsList.getLength(); i++) {
Node job = jobsList.item(i);
Element jobElement = (Element) job;
System.out.println(jobElement.getAttribute("id"));
}
for (int i = 0; i < jobsList.getLength(); i++) {
Node job = jobsList.item(i);
Element jobElement = (Element) job;
if (jobElement.getAttribute("id").equals("1")) {
rootNode.removeChild(job);
}
}
System.out.println("list after removal");
jobsList = rootElement.getElementsByTagName("job");
for (int i = 0; i < jobsList.getLength(); i++) {
Node job = jobsList.item(i);
Element jobElement = (Element) job;
System.out.println(jobElement.getAttribute("id"));
}
} catch (Exception e) {
e.printStackTrace();
}
}
output:
list before removal
1
2
list after removal
2

Java , XML User Verification Failure

I've been trying to make this work for longer than I'd like to say now and just can't figure out why it won't recognize the password,I get a null pointer exception on this line if (userPassword.getTextContent().equals(password)
Here is the method
public class XML {
public static boolean login(String email, String password) {
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document document = documentBuilder.parse("data.xml");
Element root = document.getDocumentElement();
NodeList nList = root.getChildNodes();
for (int i = 0; i < nList.getLength(); i++) {
Node nNode = nList.item(i);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element users = (Element) nNode;
if (users.getNodeName().compareTo("users") == 0) {
NodeList userList = users.getChildNodes();
for (int j = 0; j < userList.getLength(); j++) {
Node userNode = userList.item(j);
NodeList AttributeList = userNode.getChildNodes();
Node userPassword = AttributeList.item(1);
Node userEmail = AttributeList.item(0);
if (userPassword.getTextContent().equals(password)
&& userEmail.getTextContent().equals(email)) {
return true;
}
}
}
}
}
} catch (ParserConfigurationException | SAXException | IOException e) {
}
return false;
}
Attribute nodes don't have text content but a value. You should use the following construct to retrieve it :
Node userNode = userList.item(j);
String attributeValue = userNode.getAttribute("attributeName")
Alternatively since you already have the attributes Nodes, you could cast them to org.w3c.dom.Attr and use their .getValue() method.

DOM - Missing Element in XML Document

The XML file contains Employee (with empID, empName, empCode). In some situation empCode is missing.
<Employee><Detail><empID>1</empID><empName>Abhi</empName><empCode>One</empCode>
</Detail>
<Detail><empID>2</empID><empName>Amit</empName>
</Detail>
</Employee>
I am getting the Null pointer Exception while calling the getTagValue() method for "empCode" as there is no tag available with the name in XML.
Java Code :
try
{
File xmlFile = new File("New.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(xmlFile);
doc.getDocumentElement().normalize();
NodeList nList = doc.getElementsByTagName("Detail");
for (int temp = 0; temp < nList.getLength(); temp++)
{
Node nNode = nList.item(temp);
if (nNode.getNodeType() == Node.ELEMENT_NODE)
{
Element eElement = (Element) nNode;
EmpDetail empInfo = new EmpDetail();
empInfo.SetEmpID(getTagValue("empID", eElement));
empInfo.SetEmpName(getTagValue("empName",eElement));
empInfo.SetEmpCode(getTagValue("empCode",eElement));
DBConnector.SaveinDB(empInfo);
}
}
}
catch (Exception e)
{
System.out.println("Error: ");
e.printStackTrace();
}
}
private static String getTagValue(String sTag, Element eElement)
{
NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes();
Node nValue = (Node) nlList.item(0);
if(nValue == null)
return null;
return insertEscapeSequance(nValue.getNodeValue());
}
private static String insertEscapeSequance(String str)
{
String returnstr = "";
String[] strarr = str.split("'");
returnstr = strarr[0];
for(int i=1;i<strarr.length;i++)
{
returnstr = returnstr + "\\'" + strarr[i];
}
return returnstr;
}
Now I want to save the XML data into sql like this :
1 Abhi One
2 Amit null
I tried so many links but not success. Can someone please help me
If this is possible that there is no such value, then simply handle it like that:
if(getTagValue("empCode",eElement) != null){
empInfo.SetEmpCode(getTagValue("empCode",eElement));
}
Regarding to adding them into SQL, do the same check while creating your statement. As SQL NULL type exists
The problem is caused by eElement.getElementsByTagName(sTag).item(0). The statement returns the first node specified by sTag. In the case of empCode, null is returned, and calling getChildNodes() will raise a null pointer exception.
Try:
Node node = eElement.getElementsByTagName(sTag).item(0);
if (node != null) {
Node nValue = node.getChildNodes().item(0);
if (nValue != null) {
return insertEscapeSequance(nValue.getNodeValue());
}
}
return null;

Obtaining Xpath of all nodes in an xml

I have an xml. I want to obtain/print the Xpath(complete) of all the nodes in it using Java. I'm trying to use a DOM parser.
File stocks = new File("File Name");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(stocks);
System.out.println("Parsed successfully");
doc.getDocumentElement();
System.out.println("root of xml file : " + doc.getDocumentElement().getNodeName());
I'm able to get my root node to print, But not its children.
Strangely enough I just wrote a method that could be used for this. This however isn't fully namespace aware, so be warned, it also only works for ELEMENT types.
For this method to work you also need your document to be namespace aware. dbFactory.setNamespaceAware(true);. If you can't have it namespace aware then replace everywhere you see getLocalName() to getTagName().
try {
XPath xpath = XPathFactory.newInstance().newXPath();
// get all nodes in the document
NodeList nList = (NodeList) xpath.evaluate("//*", doc.getDocumentElement() ,XPathConstants.NODESET);
for(int i=0;i<nList.getLength();i++) {
if(nList.item(i).getNodeType() == Node.ELEMENT_NODE)
System.out.println(getElementXPath((Element)nList.item(i), doc.getDocumentElement()));
}
} catch (XPathExpressionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/**
* Finds the xPath relative to the given node, the relativeTo should always be a parent of elt
* #param elt
* #param relativeTo should be a parent of elt, if it isnt the path from the document root will be returned
* #return
*/
public static String getElementXPath(Element elt, Element relativeTo) {
String path = "";
do {
String xname = elt.getLocalName() + "[" + getElementIndex(elt) + "]";
path = "/" + xname + path;
if(elt.getParentNode() != null && elt.getParentNode().getNodeType() == Element.ELEMENT_NODE)
elt = (Element) elt.getParentNode();
else
elt = null;
} while(elt != null && !elt.equals(relativeTo));
return path;
}
/**
* #param original
* #return the index this element is among its siblings, only accounts for siblings with the same tag name as itself. Used for xpath indexing
*/
private static int getElementIndex(Element original) {
int count = 1;
for (Node node = original.getPreviousSibling(); node != null; node = node.getPreviousSibling()) {
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element) node;
if (element.getLocalName().equals(original.getLocalName()) &&
(element.getNamespaceURI() == original.getNamespaceURI() || (element.getNamespaceURI() != null && element.getNamespaceURI().equals(original.getNamespaceURI())))) {
count++;
}
}
}
return count;
}

xml routine not processing the child nodes I want

<report id="oleg">
<title>olegitimerade rapport</title>
<id_email>joch1</id_email>
<id_email>kang15</id_email>
</report>
I am using the following code with the DOM XML parser to extract the above list an xml file.
String tag = "report";
String[] elementTags = new String[] {"title", "id_email"};
NodeList nList = this.doc.getElementsByTagName(tag);
try{
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
HashMap<String, String> attribute = new HashMap<String, String>();
Element eElement = (Element) nNode;
if(eElement.hasAttribute("id")){
String id = eElement.getAttribute("id");
attribute.put("id", id);
for (String elementTag : elementTags){
try{
int index=0;
while(null!=eElement.getElementsByTagName(elementTag).item(index).getTextContent()){
attribute.put(elementTag, eElement.getElementsByTagName(elementTag).item(index).getTextContent());
index++;
}
}catch (Exception e){
//System.out.println("id : "+id+" - Attribute element '"+ elementTag+ "' not found in XML! ["+this.xmlFile+"]");
}
}
attributes.add(attribute);
}
}
}
}catch (Exception e) {
e.printStackTrace();
}
The problem is when I get the multiple tags....I cannot extract the 2nd ID (kang15).
Clearly the while loop is incorrect, so I need a little help here fixing the issue.
while(null!=eElement.getElementsByTagName(elementTag).item(index).getTextContent()){
attribute.put(elementTag, eElement.getElementsByTagName(elementTag).item(index).getTextContent());
index++;
}
Consider using XPath for this:
XPath xpath = XPathFactory
.newInstance()
.newXPath();
Element report = (Element) xpath.evaluate("//report", doc, XPathConstants.NODE);
String id = xpath.evaluate("#id", report);
String title = xpath.evaluate("title", report);
NodeList emails = (NodeList)
xpath.evaluate("id_email/text()", report, XPathConstants.NODESET);
System.out.println(id);
System.out.println(title);
for(int i=0; i<emails.getLength(); i++) {
System.out.println(emails.item(i).getTextContent());
}

Categories