[英]java How to convert Document to String.But the empty tag is <></>,isn't </>
In my project ,my company requires empty tag use <a></a>
form ,rather than <a/>
form.在我的项目中,我的公司要求使用<a></a>
形式的空标签,而不是<a/>
形式。 And Haven't redundant enters and spaces.并且没有多余的输入和空格。 But I don't know how to do it.但我不知道该怎么做。 You can use any way to do it.您可以使用任何方式来做到这一点。
Here is I generate xml(String) from Document:这是我从文档生成 xml(String):
< Header>< Fund_Id>1< /Fund_Id>< Trade_Code/>< Error_Code/>< Error_Message/>< /Header>
To the empty tags, I want it is form.对于空标签,我希望它是形式。 example:例子:
< Header>< Fund_Id>1< /Fund_Id>< Trade_Code>< /Trade_Code>< Error_Code>< /Error_Code>< Error_Message>< /Error_Message>< /Header>
Thank you very much.非常感谢。
I have resolved my question by this methods.我已经通过这种方法解决了我的问题。 Convert the String after converting Document to String.将 Document 转换为 String 后转换 String。
public static String xmlChange(String xmlStr)
{
if(xmlStr!=null)
{
for (int i = 0; i < xmlStr.length();)
{
int index=xmlStr.indexOf("/>",i);
if(index>0)
{
String tempStr = xmlStr.substring(i, index + 2);
int leftIndex=tempStr.lastIndexOf("<");
String old= tempStr.substring(leftIndex);
String _new=old.replace("/","")+old.replace("/","").replace("<","</");
i=index+2;
xmlStr=xmlStr.replace(old,_new);
}
else
{
break;
}
}
xmlStr=xmlStr.replaceAll("\r|\n", "");
}
return xmlStr;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.