简体   繁体   中英

Read a value from an XML file

I know how to read a simple XML file in Java using getelementbyTag but here I want to read the MAC address from here which is 01-0C-CD-01-00-34 in Java.

I am trying to use getElementsByTagName("") , but what parameter should I pass in to get the element with type="MAC-Address" ?

<Address>
    <P type="MAC-Address"xsi:type="tP_MACAddress">010C-CD-01-00-34</P>
    <P type="VLAN-ID" xsi:type="tP_VLAN-ID">000</P>
    <P type="VLAN-PRIORITY" xsi:type="tP_VLAN-PRIORITY">4</P>
    <P type="APPID" xsi:type="tP_APPID">0001</P>
</Address>

My current code is:

public static void main(String argv[]) {
    try {
        File fXmlFile = new File("C:\\Users\\User\\Desktop\\Temp\\ReadXml\\staff.xml");
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(fXmlFile);

        doc.getDocumentElement().normalize();

        NodeList nList = doc.getElementsByTagName("Address");

        for (int temp = 0; temp < nList.getLength(); temp++) {
            Node nNode = nList.item(temp);

            if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                Element eElement = (Element) nNode;
                System.out.println("MAC: " + eElement.getElementsByTagName("").item(0).getTextContent());
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

You can use xpath to find specific parts of an xml doc

        File fXmlFile = new File("C:\\Users\\User\\Desktop\\Temp\\ReadXml\\staff.xml");
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(fXmlFile);

        // Create XPathExpression
        XPathFactory xpathFactory = XPathFactory.newInstance();
        XPath xpath = xpathFactory.newXPath();
        XPathExpression expr =
                xpath.compile("/Address/P[@type='MAC-Address']/text()");
        // evaluate the Xpath and return result as a string.
        String mac = (String) expr.evaluate(doc, XPathConstants.STRING);

        System.out.println("MAC: " + mac);

The Xpath classes are all from the java.xml.xpath package

import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;

If you want to navigate the nodes you can. You don't need to normalize the document, but you do need to use the document element rather than just the doc when you look for the nodes. You can get all the 'P' tags and look for the MAC-Address. Something like:

        NodeList nList = doc.getDocumentElement().getElementsByTagName("P");

        for (int temp = 0; temp < nList.getLength(); temp++) {
            Node nNode = nList.item(temp);

            if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                Element eElement = (Element) nNode;
                if ("MAC-Address".equals(eElement.getAttribute("type"))) {
                    System.out.println("MAC: " + eElement.getTextContent());
                }
            }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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