简体   繁体   English

文档分析显示为空

[英]Document parsing shows null

I need help in the below concept. 我需要以下概念的帮助。

I want to get attributes of xref node in the code. 我想在代码中获取外部参照节点的属性。 ie id and its value, location and its value, type and its value. 即id及其值,位置及其值,类型及其值。 I am passing xml as string. 我将xml作为字符串传递。 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;

    }

}

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. 您实际上已经选择了外部参照,因为它是唯一的xml元素。 You could either wrap another object around xref, or just use the variable 'element' to get the details. 您可以将另一个对象包装在外部参照周围,也可以仅使用变量'element'获取详细信息。

ps your class name is spelt wrong: GetAtrribute -> GetAttribute ps您的类名称拼写错误:GetAtrribute-> GetAttribute

I suggest you to use XPath to find data in your XML: 我建议您使用XPath在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 例如看这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM