Results 1 to 8 of 8
  1. #1
    ark
    ark is offline Senior Member
    Join Date
    Apr 2015
    Posts
    288
    Rep Power
    5

    Default Why I cannot get the value of Text Node

    Here is code How I can get Tom & bManager etc.
    Java Code:
    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.Text;
    import org.w3c.dom.NodeList;
    import org.xml.sax.InputSource;
    
    public class Main {
    
      public static void main(String[] args) throws Exception {
        InputSource is = new InputSource();
        is.setCharacterStream(new StringReader(xmlRecords));
        
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        
        Document doc = db.parse(is);
        NodeList nodes = doc.getElementsByTagName("employee");
        for (int i = 0; i < nodes.getLength(); i++) {
          Element element = (Element) nodes.item(i);
          NodeList nl=element.getChildNodes();
          boolean n=element.hasAttribute("Tom");
          //System.out.println(n);
          for (int j = 0; j < nl.getLength(); j++) {
          if(nl.item(j).getNodeType() == 1){
          Element nn = (Element) nl.item(j);
          System.out.println(nn.getNodeName());}
          if(nl.item(j).getNodeType() != 1){
          Text nm = (Text) nl.item(j);
          System.out.println(nm.getTextContent());}
          }      
        }
      }
      static String xmlRecords = 
          "<data>" +
          "  <employee>" +
          "    <name>Tom</name>"+ 
          "    <title>Manager</title>" +
          "  </employee>" +
          "  <employee>" +
          "    <name>Jerry</name>"+ 
          "    <title>Programmer</title>" +
          "  </employee>" +
          "</data>";
    }
    What is <name> - Node name or Tag Name;
    Is Tom(Text) --child node for employee
    Java Code:
     <employee>" +
          "    <name>Tom</name>"+ 
          "    <title>Manager</title>" +
    So what if I want to search for Manager --I search by tagname<title> then how connect the tagname with the text content??

  2. #2
    ark
    ark is offline Senior Member
    Join Date
    Apr 2015
    Posts
    288
    Rep Power
    5

    Default Re: Why I cannot get the value of Text Node

    System.out.println(nm.getTextContent());} -- Sorry it works with element node nn.
    But why I cannot use just Text node method -
    Java Code:
    if(nl.item(j).getNodeType() == 3){
          Text nm = (Text) nl.item(j);
          System.out.println(nm.getWholeText());}
          }

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Why I cannot get the value of Text Node

    hy I cannot use just Text node method -
    What does line #3 print? Post the output and add some comments saying what is wrong with it and show what it should be.

    Note: When printing short values its very useful to add a String to the line to identify it from other short values:
    Java Code:
      System.out.println("WT="+nm.getWholeText());
    Last edited by Norm; 11-03-2015 at 01:32 AM.
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    ark
    ark is offline Senior Member
    Join Date
    Apr 2015
    Posts
    288
    Rep Power
    5

    Default Re: Why I cannot get the value of Text Node

    It print nothing, voidness -- you can launch this code with this line and check.

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Why I cannot get the value of Text Node

    Is an empty String a valid value?
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    ark
    ark is offline Senior Member
    Join Date
    Apr 2015
    Posts
    288
    Rep Power
    5

    Default Re: Why I cannot get the value of Text Node

    Empty String should be text value such manager

  7. #7
    ark
    ark is offline Senior Member
    Join Date
    Apr 2015
    Posts
    288
    Rep Power
    5

    Default Re: Why I cannot get the value of Text Node

    Java Code:
    <File System>
    <File>
    <Name>MSDOS.SYS</Name>
    <Size>6</Size>
    <Path>c:\MSDOS.SYS</Path>
    </File>
    <File>
    <Name>COMMAND.COM</Name>
    <Size>95202</Size>
    <Path>c:\COMMAND.COM</Path>
    </File>
    </File System>
    --if I have such xml and want to find the file by name --
    I take FileSystem as the RootElement -
    then I take Nodelist getElmentsbyTagName(FILES) and get the array of it.
    Then I traverse it with Nodelist.item(i) - but if the i - very big, how I can go through the Nodelist without FOR loop, but with WHILE loop?
    If so I also take Nodelist.item(i).getElmentsbyTagName(NAME) and check concurrently
    Nodelist.item(i).getElmentsbyTagName(NAME).getText Content if the text of name tag is the same as of input of input search field? If so - I display it JTextarea(or JLabel). But I also want to display the SIZE and PATH (foremost) of the file with needed name so how I can go
    from Name to SIZE, and then PATH to display also their text Content?
    <Name>MSDOS.SYS</Name>
    <Size>6</Size>
    <Path>c:\MSDOS.SYS</Path>
    - are there such functions as NextElement - to more from Name element to Size Element and so on.
    Or I should use just Nodelist.item(i).getElmentsbyTagName(NAME).getPare nt()"FILE".getElmentsbyTagName(SIZE)"just one item in NODELIST". getTextContent().
    Am I correct?

  8. #8
    ark
    ark is offline Senior Member
    Join Date
    Apr 2015
    Posts
    288
    Rep Power
    5

    Default Re: Why I cannot get the value of Text Node

    Quote Originally Posted by Norm View Post
    What does line #3 print? Post the output and add some comments saying what is wrong with it and show what it should be.

    Note: When printing short values its very useful to add a String to the line to identify it from other short values:
    Java Code:
      System.out.println("WT="+nm.getWholeText());
    It prints the Content of TextArea in theory but there were just blank "white" at practice

Similar Threads

  1. Getting rid of Node
    By WSUVanc in forum New To Java
    Replies: 1
    Last Post: 03-15-2013, 02:56 PM
  2. Replies: 1
    Last Post: 08-12-2012, 03:15 AM
  3. Replies: 1
    Last Post: 04-16-2012, 02:29 PM
  4. Replies: 4
    Last Post: 02-06-2009, 10:32 AM
  5. java DOM: append text to node
    By newbieal in forum New To Java
    Replies: 2
    Last Post: 10-10-2008, 07:18 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •