Document的setXmlStandalone()方法不生效

public void test throws ParserConfigurationException { DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance; DocumentBuilder docBuilder = docFactory.newDocumentBuilder; Document doc = docBuilder.newDocument; doc.setXmlStandalonetrue; //无效 Element request = doc.createElement"request"; Element sn = doc.createElement"sn"; Element user = doc.createElement"user"; Element passwd = doc.createElement"passwd"; Element args = doc.createElement"args"; Element arg1 = doc.createElement"arg"; arg1.setAttribute"name", "arg1"; Element arg2 = doc.createElement"arg"; arg2.setAttribute"name", "arg2"; Element arg3 = doc.createElement"arg"; arg3.setAttribute"name", "arg3"; sn.setTextContent"201601011234567890"; user.setTextContent"XXXX"; passwd.setTextContent"XXXX"; arg1.setTextContent"Y"; arg2.setTextContent"YY"; arg3.setTextContent"YYY"; doc.appendChildrequest; request.appendChildsn; request.appendChilduser; request.appendChildpasswd; request.appendChildargs; args.appendChildarg1; args.appendChildarg2; args.appendChildarg3; System.out.printlnXmlTools.docToStrdoc, true;
}

输出结果

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<request>
<sn>201601011234567890</sn>
<user>XXXX</user>
<passwd>XXXX</passwd>
<args>
<arg name="arg1">Y</arg>
<arg name="arg2">YY</arg>
<arg name="arg3">YYY</arg>
</args>
</request>

我是想要standalone=”yes”的,但是用doc.setXmlStandalonetrue方法行不通

发表评论

电子邮件地址不会被公开。 必填项已用*标注