Failing to get element values using Element.getAttribute()

后端 未结 4 510
北恋
北恋 2021-01-14 05:14

I would like to read an xml file. I\' ve found an example which is good until the xml element doesn\'t have any attributes. Of course i\'ve tried to look after how could I r

相关标签:
4条回答
  • 2021-01-14 05:42

    There is alot wrong with your code; undeclared variables and a seemingly crazy algorithm. I rewrote it and it works:

    import java.io.File;
    
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    
    import org.w3c.dom.Document;
    import org.w3c.dom.Element;
    import org.w3c.dom.Node;
    import org.w3c.dom.NodeList;
    
    public final class LearninXmlDoc
    {
        private static String getTagValue(final Element element)
        {
            System.out.println(element.getTagName() + " has attributes: " + element.hasAttributes());
    
            if (element.getTagName().startsWith("test"))
            {
                return element.getAttribute("w");
    
            }
            else
            {
                return element.getNodeValue();
            }
        }
    
        public static void main(String[] args)
        {
            final String fileName = "c:\\tmp\\test\\domXml.xml";
    
            readXML(fileName);
        }
    
        private static void readXML(String fileName)
        {
            Document document;
            DocumentBuilder documentBuilder;
            DocumentBuilderFactory documentBuilderFactory;
            NodeList nodeList;
            File xmlInputFile;
    
            try
            {
                xmlInputFile = new File(fileName);
                documentBuilderFactory = DocumentBuilderFactory.newInstance();
                documentBuilder = documentBuilderFactory.newDocumentBuilder();
                document = documentBuilder.parse(xmlInputFile);
                nodeList = document.getElementsByTagName("*");
    
                document.getDocumentElement().normalize();
    
                for (int index = 0; index < nodeList.getLength(); index++)
                {
                    Node node = nodeList.item(index);
                    if (node.getNodeType() == Node.ELEMENT_NODE)
                    {
                        Element element = (Element) node;
    
                        System.out.println("\tcolour : " + getTagValue(element));
                        System.out.println("\ttest : " + getTagValue(element));
                        System.out.println("-----");
                    }
                }
            }
            catch (Exception exception)
            {
                exception.printStackTrace();
            }
        }
    }
    
    0 讨论(0)
  • 2021-01-14 05:44

    If you have a schema for the file, or can make one, you can use XMLBeans. It makes Java beans out of the XML, as the name implies. Then you can just use getters to get the attributes.

    0 讨论(0)
  • 2021-01-14 05:47

    If you're looking purely to obtain attributes (E.g. a config / ini file) I would recommend using a java properties file.

    http://docs.oracle.com/javase/tutorial/essential/environment/properties.html

    If you just want to read a file create a new fileReader and put it into a bufferedReader.

    BufferedReader in = new BufferedReader(new FileReader("example.xml"));
    
    0 讨论(0)
  • 2021-01-14 06:08

    Use dom4j library.

    InputStream is = new FileInputStream(filePath);
    
    SAXReader reader = new SAXReader();
    org.dom4j.Document doc = reader.read(is);
    is.close();
    Element content = doc.getRootElement();  //this will return the root element in your xml file
    List<Element> methodEls = content.elements("element"); // this will retun List of all Elements with name "element"
    Attribute attrib = methodEls.get(0).attribute("attributeName"); // this is the "attributeName" attribute of first element with name "element"
    
    0 讨论(0)
提交回复
热议问题