How to create an XML text node with an empty string value (in Java)

后端 未结 7 1650
温柔的废话
温柔的废话 2020-12-16 16:00

I am using a Transform object to save my XML file but it seems to drop empty text nodes. Is there any way to create (and keep) a text node with an empty string

相关标签:
7条回答
  • 2020-12-16 16:38

    I'd go for a string attribute instead of tag contents. Another option is to use contents but put an attribute in case the contents is empty string.

    0 讨论(0)
  • 2020-12-16 16:39

    If you consider the XML specs, an empty string '' is not really defined amongst the set of valid characters. so it is possible that the document builder simply ignores any occurrence of characters which are not valid.

    0 讨论(0)
  • 2020-12-16 16:45

    A text node without text is not a text node.

    If you are trying to control how the XML element is serialized, <TYPE/> and <TYPE></TYPE> are equivalent, and it will not matter to an XML processor if either was used. Both are declaring a TYPE element without any text(). Most processors will serialize an empty element as a self-closing element.

    If you really want to prevent the element from being serialized as self-closing, you could get cute and add a zero-width space as the text node value: <TYPE>&#x200B;</TYPE> which will look like: <TYPE></TYPE>.

    It isn't technically an "empty" string, but might achieve what you want, and will not pad with space if the text node is selected and used.

    0 讨论(0)
  • 2020-12-16 16:46

    Here is the code for what you are looking for:

    try{
        DocumentBuilderFactory docFactory=DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder=docFactory.newDocumentBuilder();
    
        //root Elements -- Response
        Document doc=docBuilder.newDocument();
        doc.setXmlStandalone(true);
        Element response=doc.createElement("Data");
        doc.appendChild(response);
    
        // Child Element -- Play
        Element hangup=doc.createElement("Type");
        response.appendChild(hangup);
    
        //Writer the content into xml file
        TransformerFactory transformerFactory=TransformerFactory.newInstance();
        Transformer transformer=transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT,"yes");
    
        DOMSource source=new DOMSource(doc);
        StreamResult result=new StreamResult(sayOut);
        //StreamResult result=new StreamResult(System.out);
    
        transformer.transform(source,result);
        logger.info("===========XML GENERATION DON FOR HANGUP============");
    
    }catch(ParserConfigurationException pce){
        logger.error(" ==============2======== ERROR IN PRASERCONFIGURATION ===================================");
        pce.printStackTrace();
    }
    

    Output Generated By It:

    <?xml version="1.0" encoding="UTF-8"?>
    <Data>
    <Type/>
    </Data>
    

    Hope I have given right thing... although I agree with that <Type/> or <Type></Type> has no difference with respect to XML Parser.

    0 讨论(0)
  • 2020-12-16 16:56

    I've had a similar issue where when the XML was parsed back in, <Element /> was treated as null when what I needed was <Element></Element> so it would be parsed in as an empty string. I ended up working around this by adding an empty comment node to it so it ended up looking like this <Element><!----></Element>.

    Sorry if this doesn't directly answer the question but this is top answer on google so thought someone might find this useful.

    0 讨论(0)
  • 2020-12-16 16:59

    You should anotate your field with

    @XmlElement(nillable=true)
    

    Just set it to null and you will get:

    <TYPE xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
    

    It indicates that an element is empty.

    Otherwise, if you want just open and close tags, just set its value to "" (empty String). And you will get:

    <TYPE></TYPE>
    
    0 讨论(0)
提交回复
热议问题