开发者

How do I stop getNodeName() also printing the node type

I'm stumped, hopefully I've just done a dumb thing that I can fix easily.

I'm passing in a String full of XML, being 'XMLstring'. I want to get one of the elements and print the child nodes in a "name = value" on the console. The problem is that the console keeps printing garbage along with the element name that I cannot work out how to get rid of.

Anyway, this code:

try {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        InputSource is = new InputSource();
        is.setCharacterStream(new StringReader(XMLstring));
        Document doc = db.parse(is);

        NodeList nodes = doc.getElementsByTagName("client-details");
        Node node = nodes.item(0);

        NodeList client_details = node.getChildNodes();

        for (int i = 0; i < client_details.getLength(); i++) {
            System.out.pri开发者_如何学Cntln(client_details.item(i).getNodeName()+" = "+getTextContents(client_details.item(i)));
        }
    }
    catch (Exception e) {
        e.printStackTrace();
    }

Gives me the following:

#text = 
testing-mode = false
#text = 
name = testman
#text = 
age = 30

Why is it printing the "#text ="? How do I get rid of it?

I am using NetBeans if that helps.


You want to use getNodeValue() instead:

System.out.println(client_details.item(i).getNodeValue()+" = "+getTextContents(client_details.item(i)));

If you look in the table at the top of this page, you see that for Text nodes, getNodeName() returns #text.


I am curious to see what each of the two function calls in your System.out.println() is printing out separately, only because the entire output should be on one line. One of those two is causing the problems, and i believe it may be internal to the function.

Otherwise, if you use String splitString = string.split("[=]"); it will split up the line based on the delimeter '='

then you can

    String splitString = string.split("[=]");
    System.out.println(splitString[1] + " = " + splitString[2]);

or, much more simply, make that one small edit that @retrodrone posted


OK I managed to resolve this issue, for anyone else who cares. The problem with the code is that the Node needs to be cast to an Element before you can get the tag name out of it in this manner. Therefore:

try {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        InputSource is = new InputSource();
        is.setCharacterStream(new StringReader(XMLstring));
        Document doc = db.parse(is);

        NodeList nodes = doc.getElementsByTagName("client-details");
        Node node = nodes.item(0);

        NodeList client_details = node.getChildNodes();

        Element elementary;

        for (int i = 0; i < client_details.getLength(); i++) {
            if(client_details.item(i).getNodeType() == Node.ELEMENT_NODE) {
                elementary = (Element) client_details.item(i);

                System.out.println(elementary.getTagName()+" = "+getTextContents(client_details.item(i)));
            }
        }
    }

Which produces the desired result, minus that "#text" bollocks :)

testing-mode = false
name = testman
age = 30

Notice the new "if" statement I added inside the for loop and the cast of the node to an element before calling getNodeName, which does work for Elements.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜