English 中文(简体)
How to prevent xml transformer to transform empty tags into single tag
原标题:

I m using javax.xml.transform.Transformer class to transform the DOM source into XML string. I have some empty elements in DOM tree, and these become one tag which I don t want.

How do I prevent <sampletag></sampletag> from becoming <sampletag/>?

问题回答

I hade the same problem. This is the function to get that result.

public static String fixClosedTag(String rawXml){

    LinkedList<String[]> listTags = new LinkedList<String[]>(); 
    String splittato[] =  rawXml.split("<");

    String prettyXML="";

    int counter = 0;
    for(int x=0;x<splittato.length;x++){
        String tmpStr = splittato[x];
        int indexEnd = tmpStr.indexOf("/>");
        if(indexEnd>-1){
            String nameTag = tmpStr.substring(0, (indexEnd));
            String oldTag = "<"+ nameTag +"/>";
            String newTag = "<"+ nameTag +"></"+ nameTag +">";
            String tag[]=new String [2];
            tag[0] = oldTag;
            tag[1] = newTag;
            listTags.add(tag);
        }
    }
    prettyXML = rawXml;

    for(int y=0;y<listTags.size();y++){
        String el[] = listTags.get(y);

        prettyXML = prettyXML.replaceAll(el[0],el[1]);
    }

    return prettyXML;
}

If you want to control how XML is formatted, provide your own ContentHandler to prettify XML into "text". It should not matter to the receiving end (unless human) whether it receives <name></name> or <name/> - they both mean the same thing.

The two representations are equivalent to an XML parser, so it doesn t matter.

If you want to process XML with anything else than an XML-parser, you will end up with a lot of work and an XML-parser anyway.

If the process you are sending it through NEEDS the element not to be self-closing (which it should not), you can force the element not to be self-closing by placing content inside of it.

How does the PDF converter handle XML comments or processing instructions?

<sampletag>!<--Sample Comment--></sampletag>

<sampletag><?SampleProcessingInstruction?></sampletag>

I tried below to prevent transform empty tags into single tag :

Transformer tf = TransformerFactory.newInstance().newTransformer(); tf.setOutputProperty(OutputKeys.METHOD,"html")

It s retaining empty tags.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签