gpt4 book ai didi

java - 详细格式化程序 : code snippet cannot refer to other classes whose import is not declared

转载 作者:行者123 更新时间:2023-12-02 02:05:40 24 4
gpt4 key购买 nike

我正在使用 Eclipse Oxygen.3a

我有一个类,它是 XML 文件的表示。此类没有重写toString()。我想使用 Detail Formatter 来显示它所表示的 XML 的 pretty-print 格式。

详细格式化程序代码片段:

try{
String xml = getXmlString(); // This method returns single line string representation of the XML
final InputSource src = new InputSource(new StringReader(xml));
final Node document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(src).getDocumentElement();
final Boolean keepDeclaration = Boolean.valueOf(xml.startsWith("<?xml"));
final DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
final DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS");
final LSSerializer writer = impl.createLSSerializer();

writer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE); // Set this to true if the output needs to be beautified.
writer.getDomConfig().setParameter("xml-declaration", keepDeclaration); // Set this to true if the declaration is needed to be outputted.

return writer.writeToString(document);
}
catch (Exception e) {
return getXmlString();
}

问题是,我定义的类没有我在代码片段中使用的许多类的导入声明。这会导致以下错误:

Detail formatter error:
InputSource cannot be resolved to a type
InputSource cannot be resolved to a type
StringReader cannot be resolved to a type
Node cannot be resolved to a type
DOMImplementationRegistry cannot be resolved to a type
DOMImplementationRegistry cannot be resolved
DOMImplementationLS cannot be resolved to a type
DOMImplementationLS cannot be resolved to a type
LSSerializer cannot be resolved to a type

如何实现这一目标?如果Detail Formatter不能支持这一点,还有其他方法可以实现吗?

注意 这个类是一个遗留类,我正在尝试为其编写详细格式化程序。我有源代码,但我无法对此类进行任何更改。

最佳答案

使用完整的类路径。
例如:

java.lang.String xml = getXmlString();

关于java - 详细格式化程序 : code snippet cannot refer to other classes whose import is not declared,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50838300/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com