Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9196249
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:48:35+00:00 2026-06-17T21:48:35+00:00

Hey so I’m wondering how it is possible to write a empty tag for

  • 0

Hey so I’m wondering how it is possible to write a empty tag for example <Category /> with the DocumentBuilderFactory (based off this resource, library javax.xml.parsers.*;), as for the moment I’m having to apply an if condition if object.getCategory() != null then create the Category Tag otherwise ignore it.

//add the Category
if(excel.getCategory() != null){
    Element Category = doc.createElement("category");
    Category.appendChild(doc.createTextNode(excel.getCategory()));
    Rows.appendChild(Category);
}

And Schema

<xs:complexType name="data">
    <xs:all>
    <xs:element name="Category" type="xs:string" minOccurs="1" />
    <!-- other columns.. -->
    </xs:all>
</xs:complexType>

And I’ve noticed if I add a textnode that is null, the transformer.transform(source, result); will return with a heap of NullException errors. Is there a way to configure the transformer to know that the TextNode is intentionally left empty? and in turn create <Category /> or <Category></Category>.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T21:48:36+00:00Added an answer on June 17, 2026 at 9:48 pm
    //add the Category
    Element Category = doc.createElement("category");
    Rows.appendChild(Category);
    if(excel.getCategory() != null){
        Category.appendChild(doc.createTextNode(excel.getCategory()));
    }
    

    Here I’m adding the category element to Rows unconditionally, but only adding a text node child if getCategory() is non-null. If it is null, then this will create an empty category element, which will serialize to XML as <category />.

    If you want to be able to distinguish in the XML between a null value for excel.getCategory() and an empty string value, then the usual XML schema idiom for that is to make the element “nillable”

    <xs:complexType name="data">
        <xs:all>
        <xs:element name="Category" type="xs:string" nillable="true" />
        <!-- other columns.. -->
        </xs:all>
    </xs:complexType>
    

    and mark it with xsi:nil

    //add the Category
    Element Category = doc.createElement("category");
    Rows.appendChild(Category);
    if(excel.getCategory() != null){
        Category.appendChild(doc.createTextNode(excel.getCategory()));
    } else {
        Category.setAttributeNS(XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI,
                                "xsi:nil", "true");
    }
    

    This will produce

    <category />
    

    when excel.getCategory().equals("") and

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

    when excel.getCategory() == null

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey all, just wondering what the best way to add this capability was. I
Hey all i am wondering why i am getting this error with the following
Hey i was wondering do you need root to do this any more? I
Hey guys this is my html code: <div class=nakupy> <li class=icn_kategorie><a href=#>Nákupy</a> <div class=sub_menu>
hey guys having this really simple problem but cant seem to figure out have
Hey Guys If I do this in mono touch: var alert = new UIAlertView
Hey guys i am trying to run this query in my postgres db but
Hey I am trying to figure out what units of time this is measured
Hey all. I've been been trying to figure this out for a while now.
Hey there, Now im working this base on dmitko tutorial on extending django-registration post

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.