简体   繁体   中英

DomParser from string XML getting null document

I'm trying to parse a xml string using domParser but when I trying to get the document it shows [#document: null] and it doesn't contain the data of xml passing.

The code is something like that:

Document doc = null;
DOMParser parser = new DOMParser();
logger.debug("Parsing");
InputSource IS = new InputSource(new StringReader(nameFile));            
parser.parse(IS);
doc = parser.getDocument();
NodeList NL = doc.getElementsByTagName("element");

The problem starts when doc = parser.getDocument(). It returns [#document=null]. So the NodeList can't find the element that I'm looking for. My XML is quite big. It contains around 50K character. My question is, what are the possible issue that introducing this problem?

For your information, this application with the same code works in OAS with JDK1.4 now I'm transfering the application to Weblogic 12c with JDK 1.6.

Thanks in advance.

UPDATED: Sorry for not mentioning nameFile data type. nameFile is a xml data in string format.

UPDATED2: I've tried with a simple xml but no luck. Example: 1st Example: this string is without any space ->

nameFile = "<?xml version='1.0'?><company><staff id='1001'><firstname>yong</firstname><lastname>mook kim</lastname><nickname>mkyong</nickname><salary>100000</salary></staff><staff id='2001'><firstname>low</firstname><lastname>yin fong</lastname><nickname>fong fong</nickname><salary>200000</salary></staff></company>";

2nd Example:

nameFile = "<message>Hello</message>

None of this is working. Always returns [#document:null]

I assume 'nameFile' in your code snippet is a string! The following works perfectly for me.

String nameFile= "<message>HELLO World</message>";
DOMParser parser = new DOMParser();
try {
    parser.parse(new InputSource(new java.io.StringReader(nameFile)));
    Document doc = parser.getDocument();
    String message = doc.getDocumentElement().getTextContent();
    System.out.println(message);
} catch (SAXException e) {
    // handle SAXException 
} catch (IOException e) {
    // handle IOException 
}

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