1

I have seen many similar questions on stackoverflow and tried a lot but still no success. So posting my problem.

Here is my program:

  1. Get the http output of a response which is in xml.
  2. Store the response in a string.
  3. Parse the string using xml dom parser.
  4. Fetch the element.

    <RESULTS>
<DEVICE name="qwewewew">
<PORT name="ilo">
<DB_SOURCE>abncd</DB_SOURCE>
<WIRE_TYPE>ilo</WIRE_TYPE>
<CONNECTS>abncd</CONNECTS>
</PORT>
<PORT name="onboard-1">
<DB_SOURCE>abncd</DB_SOURCE>
<WIRE_TYPE>net</WIRE_TYPE>
<CONNECTS>abncd</CONNECTS>
</PORT>
<PORT name="abncd">
<DB_SOURCE>abncd</DB_SOURCE>
<WIRE_TYPE/>
<CONNECTS/>
</PORT>
<PORT name="abncd">
<DB_SOURCE>abncd</DB_SOURCE>
<WIRE_TYPE>fiber</WIRE_TYPE>
<CONNECTS>abncd</CONNECTS>
</PORT>
<PORT name="power">
<DB_SOURCE>abncd</DB_SOURCE>
<WIRE_TYPE/>
<CONNECTS/>
</PORT>
<PORT name="serial">
<DB_SOURCE>abncd</DB_SOURCE>
<WIRE_TYPE>serial</WIRE_TYPE>
<CONNECTS>abncd</CONNECTS>
</PORT>
</DEVICE>
</RESULTS>

Snippet of My program is as follows:

String baseUrl = "http://abcd.eng.xyz.com/wiremap/index.php?action=search&page=0&port_name=&dev_type=like&output=xml&dev_name=w2-fiqa-058";
String xmlRecords = get(baseUrl).asString();

DocumentBuilderFactory factory2 = DocumentBuilderFactory.newInstance();

factory2.setNamespaceAware(true);
DocumentBuilder builder2 = factory2.newDocumentBuilder();
Document document = builder2.parse(new ByteArrayInputStream(xmlRecords.getBytes()));
String test = document.getTextContent();
System.out.println("Value " +test);

System.out.println(document);

Here document is returning null. I am not sure what I am missing.

2
  • Everything looks fine to me assuming you are getting a response in the 'xmlRecords' variable. Are you get a null pointer exception because 'document' is null? Or is 'getTextContent()' is returning null? Commented Oct 13, 2015 at 9:12
  • i m not bother about getTextContent(). I have just tried printing it. I am getting null pointer exceptin because document is null. Commented Oct 13, 2015 at 9:16

1 Answer 1

1

The org.w3c.dom.Node#getTextContent method promises to return null when invoked on:

  • DOCUMENT_NODE
  • DOCUMENT_TYPE_NODE
  • NOTATION_NODE

See docs here.

If you want to verify your Document contains what's expected, or generally iterate over its nodes, you can iterate over getChildNodes.

Here's a little recursive method that'll print some debugging info for that XML string, in a self-contained example.

package test;

import java.io.ByteArrayInputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Main {

    public static void main(String[] args) throws Exception {
        String test = "<RESULTS><DEVICE name=\"qwewewew\"><PORT name=\"ilo\"><DB_SOURCE>abncd</DB_SOURCE><WIRE_TYPE>ilo</WIRE_TYPE><CONNECTS>abncd</CONNECTS></PORT><PORT name=\"onboard-1\"><DB_SOURCE>abncd</DB_SOURCE><WIRE_TYPE>net</WIRE_TYPE><CONNECTS>abncd</CONNECTS></PORT><PORT name=\"abncd\"><DB_SOURCE>abncd</DB_SOURCE><WIRE_TYPE/><CONNECTS/></PORT><PORT name=\"abncd\"><DB_SOURCE>abncd</DB_SOURCE><WIRE_TYPE>fiber</WIRE_TYPE><CONNECTS>abncd</CONNECTS></PORT><PORT name=\"power\"><DB_SOURCE>abncd</DB_SOURCE><WIRE_TYPE/><CONNECTS/></PORT><PORT name=\"serial\"><DB_SOURCE>abncd</DB_SOURCE><WIRE_TYPE>serial</WIRE_TYPE><CONNECTS>abncd</CONNECTS></PORT></DEVICE></RESULTS>";
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document document = builder.parse(new ByteArrayInputStream(test.getBytes("UTF-8")));
        NodeList list = document.getChildNodes();
        recurse(list);
    }
    static void recurse(NodeList list) {
        if (list == null || list.getLength() == 0) {
            return;
        }
        else {
            for (int i = 0; i < list.getLength(); i++) {
                Node item = list.item(i);
                System.out.println(item);
                recurse(item.getChildNodes());
            }
        }
    }

}

Output

[RESULTS: null]
[DEVICE: null]
[PORT: null]
[DB_SOURCE: null]
[#text: abncd]
[WIRE_TYPE: null]
[#text: ilo]
[CONNECTS: null]
[#text: abncd]
[PORT: null]
[DB_SOURCE: null]
[#text: abncd]
[WIRE_TYPE: null]
[#text: net]
[CONNECTS: null]
[#text: abncd]
[PORT: null]
[DB_SOURCE: null]
[#text: abncd]
[WIRE_TYPE: null]
[CONNECTS: null]
[PORT: null]
[DB_SOURCE: null]
[#text: abncd]
[WIRE_TYPE: null]
[#text: fiber]
[CONNECTS: null]
[#text: abncd]
[PORT: null]
[DB_SOURCE: null]
[#text: abncd]
[WIRE_TYPE: null]
[CONNECTS: null]
[PORT: null]
[DB_SOURCE: null]
[#text: abncd]
[WIRE_TYPE: null]
[#text: serial]
[CONNECTS: null]
[#text: abncd]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.