简体   繁体   中英

Java DOM trim white space in XML file

I'm using DOM parer with Java. This is my simple xml file with who I will introduce my issue:

<class>
    <student rollno="393">
        <firstname> Some Name </firstname>
        <lastname>Some Family</lastname>
        <nickname>dinkar</nickname>
        <marks>85</marks>
        <address>
            <street>Some Street</street>
            <number>71</number>
        </address>
    </student>
</class>

This is my part from java code with who I have tried to parse my xml file:

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

            for (int i = 0; i < nList.getLength(); i++)
            {

                Node n = nList.item(i);
                Element e = (Element)n;;

                if(e.getAttribute("rollno").equals("393")){
                    log.debug(e.getFirstChild().getNodeName().trim());
                }
            }
        }

I'm trying to get first child node from my xml. But when I run program I received the result #Text. This is because of space between student and first name elements. How can I trim this space and receive result firstname.

Thanks for the time spend for my issue.

Thanks to VGR for the help. There is the correct source for my issue.

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

                for (int i = 0; i < nList.getLength(); i++)
                {

                    Element e = (Element)nList.item(i);

                    if(e.getAttribute("rollno").equals("393")){
                        log.debug(e.getFirstChild().getNodeName().trim());
                    }
                }
            }

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