Read a value from an XML file

Multi tool use
Multi tool use












-3















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();
}
}









share|improve this question





























    -3















    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();
    }
    }









    share|improve this question



























      -3












      -3








      -3








      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();
      }
      }









      share|improve this question
















      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();
      }
      }






      java xml-parsing






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 2 at 16:00









      DaveyDaveDave

      3,77783758




      3,77783758










      asked Jan 2 at 15:38









      SabujSabuj

      11




      11
























          1 Answer
          1






          active

          oldest

          votes


















          2














          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());
          }
          }
          }





          share|improve this answer
























          • It worked thank you so much

            – Sabuj
            Jan 2 at 19:06











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54009110%2fread-a-value-from-an-xml-file%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          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());
          }
          }
          }





          share|improve this answer
























          • It worked thank you so much

            – Sabuj
            Jan 2 at 19:06
















          2














          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());
          }
          }
          }





          share|improve this answer
























          • It worked thank you so much

            – Sabuj
            Jan 2 at 19:06














          2












          2








          2







          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());
          }
          }
          }





          share|improve this answer













          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());
          }
          }
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 2 at 17:51









          pcoatespcoates

          1,0351210




          1,0351210













          • It worked thank you so much

            – Sabuj
            Jan 2 at 19:06



















          • It worked thank you so much

            – Sabuj
            Jan 2 at 19:06

















          It worked thank you so much

          – Sabuj
          Jan 2 at 19:06





          It worked thank you so much

          – Sabuj
          Jan 2 at 19:06




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54009110%2fread-a-value-from-an-xml-file%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          r Is79 i9PQbIgakR7Y7QCeiNNsQN8vvFyA,Zn0m xSOyh UNFmzgoj7zJDfH21GACXUFabYFY8p3Y0iB9J369Yo Jp Ykv
          Yr0MNbOx4HpcYSnr 1o mrQ,aNPgYCFniL,0a awjUv,DhOC2,M sFhg5BpY3jceS,2im

          Popular posts from this blog

          Monofisismo

          Angular Downloading a file using contenturl with Basic Authentication

          Olmecas