简体   繁体   English

java如何将Document转换为String。但是空标签是&lt;&gt;</> ,不是</>

[英]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.

相关问题 我想将二进制字符串转换为相应的十六进制字符串,但是当输入的二进制字符串很长时,则会显示NumberFormatException。如何解决? - I want to convert a binary string to corresponding hex string.But when the input binary string long then NumberFormatException is shown.How to fix it? 如何在java中将String转换为DOM Document对象? - How to convert String to DOM Document object in java? 如果不为空,如何清除BufferedReader [Java / Android] - How to clear BufferedReader if it isn't empty [Java/Android] 如果没有在另一个字符串上绘制字符串,如何检查Java? - How to check in Java if a string isn't drawn over another string? 如何从Mongo Document转换为Java Set <String> ? - How to convert from Mongo Document to java Set<String>? 如何在Java中将RFID标签数据(Askii)转换为字符串 - how to convert rfid tag data (askii) to string in JAVA 如何使用Java将带有img标签的HTML字符串转换为itext pdf - how to convert HTML string with img tag to itext pdf using java 在Java中将String XML片段转换为Document Node - Convert String XML fragment to Document Node in Java 字符串替换不适用于for循环 - Java - String replace isn't working in for loop - Java 检查字符串是否为空文本或数字的方法不起作用 - Method for checking if string is getting empty text or numbers isn't working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM