0

I need help in the below concept.

I want to get attributes of xref node in the code. i.e id and its value, location and its value, type and its value. I am passing xml as string. But the document shows null on parsing.

PLease help me in this.

import java.io.StringReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;



public class GetAtrribute {

    /**
     * @param args
     */
    public static void main(String[] args) {
        String xml = "<xref id=\"19703675\" location=\"abstract\" type=\"external\">PubMed Abstract: http://www.abcd.nlm.nih.gov/...</xref>"; //Populated XML String....
        GetAtrribute ga = new GetAtrribute();
        try {
            ga.getValues(xml);
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    public String getValues(String xmlStr) throws Exception {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder;
        xmlStr = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + xmlStr;
        try {
            builder = factory.newDocumentBuilder();
            Document document = builder.parse(new InputSource(new StringReader(
                    xmlStr)));
            Element element = document.getDocumentElement();

            NodeList list = element.getElementsByTagName("xref");
            if (list != null && list.getLength() > 0) {
                NodeList subList = list.item(0).getChildNodes();

                if (subList != null && subList.getLength() > 0) {
                    return subList.item(0).getNodeValue();
                }
                for (int count = 0; count < subList.getLength(); count++) {
                    System.out.println(subList.item(count).getNodeValue());
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return xmlStr;

    }

}
Manushi
  • 597
  • 1
  • 7
  • 26

2 Answers2

0

Your problem is that when you run this line:

Element element = document.getDocumentElement();

you're actually selecting xref already, because its the only xml element. You could either wrap another object around xref, or just use the variable 'element' to get the details.

p.s. your class name is spelt wrong: GetAtrribute -> GetAttribute

M21B8
  • 1,867
  • 10
  • 20
0

I suggest you to use XPath to find data in your XML:

XPath xPath = XPathFactory.newInstance().newXPath();
Document baseDoc;
try (InputStream pStm = new ByteArrayInputStream(baseXmlString.getBytes("utf-8"))) {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    baseDoc = builder.parse(pStm);
} catch (SAXException | IOException | ParserConfigurationException ex) {
    getLogger().error(null, ex);
    return null;
}
try {
    XPathExpression expression = xPath.compile(xPathExpression);
    return (T) expression.evaluate(baseDoc, pathType);
} catch (XPathExpressionException ex) {
    getLogger().error(null, ex);
}
return null;

For example take a look at here

Community
  • 1
  • 1
Maksim
  • 449
  • 4
  • 11