简体   繁体   English

字符串XML中的DomParser获取空文档

[英]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. 我正在尝试使用domParser解析xml字符串,但是当我尝试获取文档时,它显示[#document:null],并且其中不包含xml传递的数据。

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(). 当doc = parser.getDocument()时,问题开始。 It returns [#document=null]. 它返回[#document = null]。 So the NodeList can't find the element that I'm looking for. 因此,NodeList找不到我要寻找的元素。 My XML is quite big. 我的XML很大。 It contains around 50K character. 它包含大约50K字符。 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. 供您参考,具有相同代码的该应用程序可在带有JDK1.4的OAS中运行,现在我将其与JDK 1.6一起转移到Weblogic 12c。

Thanks in advance. 提前致谢。

UPDATED: Sorry for not mentioning nameFile data type. 更新:很抱歉没有提到nameFile数据类型。 nameFile is a xml data in string format. nameFile是字符串格式的xml数据。

UPDATED2: I've tried with a simple xml but no luck. UPDATED2:我尝试使用简单的xml,但是没有运气。 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] 始终返回[#document:null]

I assume 'nameFile' in your code snippet is a string! 我假设您的代码段中的“ nameFile”是一个字符串! 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 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM