How to ignore empty text element while using DOM to parse xml??

hi everyone,
i am using DOM to parse an xml file. But i dont know how to cinfig the DocumentBuilderFactory to ignore empty text elements.
For example, i have an xml file like this:
<?xml version="1.0" encoding="UTF-8" ?>
<root>
    <child>Tom</child>
    <child>Jerry</child>
</root>I used the following codes to parse:
String fname = "Tom-and-Jerry.xml";
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setIgnoringElementContentWhitespace(true);
factory.setIgnoringComments(true);
DocumentBuilder builder = domFactory.newDocumentBuilder();
// Generate a DOM tree from the DOM builder.
org.w3c.dom.Document dom = builder.parse(new File(fname));
org.w3c.dom.NodeList list = dom.getChildNodes();
for (int i=0; i<list.getLength(); i++) {
    System.out.println("Child No."+i);
    System.out.println("NodeName="+list.item(i).getNodeName());
    System.out.println("NodeType="+getType(list.item(i).getNodeType()));
    System.out.println("NodeValue="+list.item(i).getNodeValue());
    System.out.println();
}The result is not exactly what i want ---- there are 5 children in list!! The 1st, 3rd and 5th are #text and their values are all empty. Only the 2nd and the 4th are the child that i expect.
It is really troublesome to get all these silly empty texts as sub elements. I tried to get rid of them, but i failed. I just dont understand why factory.setIgnoringElementContentWhitespace(true) did not work.
Anyone can help me? thanks.
Heavy ZHENG

I just dont understand why factory.setIgnoringElementContentWhitespace(true) did not work.That only does something if the XML has a DTD that enables it to know what whitespace can be ignored and what is significant. The API documentation for the method refers you to this document:
http://www.w3.org/TR/REC-xml#sec-white-space

Similar Messages

  • Problems while using KXML2 to parse XML file

    I have been doing a lot of reading up on the APIs of XMLPull and KXML2.
    I seem to be facing a problem which can't be solved just by reading but rather the expertise of the more experienced programmers dealing with XML parsing in J2ME.
    I know how long it takes to process an entire XML document, I just would like to parse certain parts of the document. For e.g. instead of reading the whole document, say I just want to read the first 7 items and subsequently, the next 7 items when the user clicks on the next page?
    In other words, I wish to incorporate paging. Is it possible? Or if you have an alternative solution which will not make a user wait too long, please share it!
    Can anyone help me?

    Hi,
    I passed the full path with file name to SAXParser as follow:
    SAXParserFactory factory = SAXParserFactory.newInstance();
    SAXParser sap = factory.newSAXParser();
    XMLReader parser = sap.getXMLReader();
    sap.parse( new File(importFile) );
    // Specify that we don't want validation. This is the SAX2
    // API for requesting parser features.
    parser.setFeature("http://xml.org/sax/features/validation", true);
    // Instantiate this class to provide handlers for the parser and
    // tell the parser about the handlers
    Import handler = new Import();
    parser.setContentHandler(handler);
    parser.setErrorHandler(handler);
    // Create an input source that describes the file to parse.
    // Then tell the parser to parse input from that source
    org.xml.sax.InputSource input = new InputSource(new FileReader(importFile));
    parser.parse(input);
    Where "importFile" is : /tmp/testFile.xml

  • SAP Script - How to check a text element is empty in Script.

    Hello Friends,
                  I like to display a text element when it is not empty, otherwise I like to display some other text.
    I know how to read text element in Script, but 
    How to check a text element is empty in Script
    Thank you for your time.
    Senthil

    Hi Senthil,
    Inside text element, we print the variables.
    Variables actually gets printed in the form.
    if the variable contains value the variable gets printed on script. if not, then variable not gets printed.
    hope this helps.

  • How to validate an text field item using javascript for numbers only.

    hi,
    how to validate an text field item using javascript for numbers only.please help me on this urgent
    please mail me solun if posible on [email protected]

    Hi,
    Page HTML header
    <script>
    function onlyNum(evt) {
      // Usage: onKeyPress="return onlyNum(event)"
      evt = (evt) ? evt : window.event;
      var charCode = (evt.which) ? evt.which : evt.keyCode;
      if (charCode > 31 && (charCode < 48 || charCode > 57)) {
        var status = 'This field accepts numbers only!';
        alert(status);
        return false;
      var status = '';
      return true;
    </script>Item HTML Form Element Attributes
    onKeyPress="return onlyNum(event)"Br,Jari

  • How to transport the  text elements

    hi guru's,
      i need answer to this question.
      how to transport the text elements in the sap script?

    Hi,
    U mean Standard Text?
    Standard Text can create using SO10.
    Use RSTXTRAN Standard Program..
    Do Like this..
    Goto se38 and enter program name as RSTXTRAN
    and press F8..
    and give Text-key Name as Standard Text Name
    then Press F8..
    And select Select check box.. and enter
    and press on trsfr to corr button..
    it asks transfee textxs to a correction..
    press on yes button..
    it will show request number or create your own request...
    and after goto SE10 tcode and Transport it...
    Regards,
    KK

  • How to color the text  elements in script?

    how to color the text  elements in script?
    byeeeee
    plz send the answers

    Hi,
    Please go through my blog to know how to do color printing in SAP scripts.
    White Paper on 'Color Printing in SAP Scripts'
    Regards,
    Sireesha Ch

  • How to transport the text elements in  Reports?

    need answer to this question.
    how to transport the text elements in the reports?
    Edited by: venkata kumar on Feb 18, 2008 1:33 PM

    Hi Venkata,
    Text Elements are a component/part of the SAP script and are not individual ABAP objects within the SAP environment hence they cannot be transported independently.
    You would need to make changes in your text elements in the SAP script which would result in the SAP script being saved under a transport and moving the transport across the landscape would result in the text elements also getting transported.
    In short: Move SAP script - to move text elements.
    Cheers,
    Aditya

  • Hi..how we enter standard text for smartform using include text

    hi
    guru
    ..how we enter standard text for smartform using include text..
    i know only using so10 we write text..
    in smartform it asking text-object
                                     text-id,
    so what values we should give to get text

    Hi,
       Do u want to print Standard Text or the Texts that are maintained at the Transaction level.
    If it is standard text , then u use text id as ST or if it is Texts that are maintained at the Transaction level then u have to go to that specific transaction and check for that text , then go to text editor ,in the goto menu u have header data where u get the Text id , text name.... , use them in ur smartform in the INCLUDE text.
    Further u have the option of text modules in smartforms which act as standard texts, so if u r going for creation of standard texts then its better to use text modules.
    Regards,
    Shafivullah Mohammad

  • How do I sort or reorganize while using the new Numbers 3.0

    How do I sort or reorganize while using the new Numbers 3.0; I can't seem to find the sort icon.
    Thanks
    ActiveSolutions

    Yes, I understand the convenience of the old Reorganize panel. I thought it was great. The problem is (I'm guessing) that it is really hard to do something like that in Numbers iOS and Numbers for iCloud. So they gave us features that are more likely to work across all platforms.
    If you need to sort just selected rows you could put the =A&C&B formula (or whatever order you need) in just the rows you need to sort. Leave the other cells in column D blank.
    Or, if you're doing a LOT of partial sorting like that maybe you should have the data in separate tables anyway.
    Not saying it's great... just saying there are workarounds... until (if) something like the Reorganize panel comes back by popular acclaim. 
    SG

  • How to improve the load performance while using Datasources for the Invoice

    HI All,
    How to improve the  load performance while using Datasources for the Invoice . Actually my invoice load (Appx. 0.4 M records) is taking very long time nearly ~16 to 18 hrs  to update data from R/3 to 0ASA_DS01.
    If I load through flat file it will load with in ~20 Min for the same amount of data.
    Please suggest how to improve load performance.
    PS: I have done the Inpo package settings as per the OSS note.
    Regads
    Srininivasarao.Namburi.

    Hi Srinivas,
    Please refer to my blog posting [/people/divyesh.jain/blog/2010/07/20/package-size-in-spend-performance-management-extraction|/people/divyesh.jain/blog/2010/07/20/package-size-in-spend-performance-management-extraction] which gives the details about the package size setting for extractors. I am sure that will be helpful in your case.
    Thanks,
    Divyesh
    Edited by: Divyesh Jain on Jul 20, 2010 8:47 PM

  • How to print the texts retrived by using READ_TEXT fun module in Smartform

    Please tell me how to print the text which is rertrived by using the READ_TEXT function module in smartform.
    I have coded all things in the program lines and in that i am retriveing the long texts.
    I am getting the text lines in my internal table clearly, the thing is that I am not able to pass these lines to the text.
    I have to print the trouble ticket. in that the notes log I have to pass.
    its urgent. Points will be rewarded for any type of clue. whether it will work or not.

    There are a few ways to do it. If you need to take all of the text in the text type, in your SF text element choose "Include Text".
    Populate the fields with the data that corresponds to the text type. It is similar to the interface to the FM "Read_Text.
    Text Name
    Text Obje
    Text ID 
    Language
    Encase any variables with the "&" symbol.
    If you have already coded the call to the FM "READ_TEXT" and loaded the text into an internal table, create a loop and loop through the itab. Inside of the loop create a text element and add a variable in the text element for the field you are looking to output.

  • How to copy a text element to another in sapscript

    Question
    Have a bug in the system and need to copy an existing text element to a new one.
    How do you do this.
    I have found the text elelement in tihe MAIN window - the code /E   510-C  <-- this needs to be copied to a new text element called /E    510-A.  I can cust and paste this into the text element screen  But doesn't the 510-A refer to some text created via SO10?  But then doesn't sapscript require you to use an include to get the text in there?
    HELP - 2 days from go -live here!!
    Thanks and big points to who can ever help!

    Text elements are specified with "/E".  These are different from STANDARD TEXT objects that you define in SO10 and reference with the INCLUDE statment.
    Text elements are called from the ABAP print program.  If you copy text element 510-C to 510-A, the ABAP print program has to call element 510-A to execute the statements in this section.
    Are you dealing with errors from a syntax check?

  • CS3/4, JS Amateur question: how remove all empty text frames?

    I'm an amateur, and thought it would be a straightforward matter to write a script that simply deletes all empty text frames in all of a document's stories. Here's my second attempt:
    var myDocument = app.activeDocument;
    var myStories = myDocument.stories;
    for (i = 0; i < myStories.length; i++){
      var myStory = myStories[i];
      var myTextFrames = myStory.textContainers;
        for (j = myTextFrames.length - 1; j >=0; j--) {
        if (myTextFrames[j].contents == "") myTextFrames[j].remove();
    The trouble is, sometimes it works as intended, but sometimes, with multiple stories, it needs to be run more than once. Can anyone explain why that is?
    Thanks -- Jeremy

    [Jongware] wrote:
    (Bit of a Johnny-come-lately, but anyway )
    AHA! -- This is a great help. It was driving me up the wall, and I don't know why it didn't work for me last night. Maybe it's because I was counting through stories in the wrong direction. Anyhow, I see how it works now, and I'm very grateful for the help.
    By the way Harbs, I used all three of your suggestions because I had spent far too much time tapping away at that nut with my little toy hammer. I vented my frustration by using all three of your sledgehammers. In particular, I made sure it left a single empty text frame where it was, as such a frame might be used as a graphic. (Although in the past, I've found an empty story can be a sneaky place for a rogue font to hide. The insertion point just in front of the "end of story" character can have a paragraph style applied, with its associated font, but "Find" won't find any characters with that paragraph style or font applied!)
    Thanks to all again -- Jeremy

  • Ignoring the DOCTYPE element while parsing the xml

    I am using JAXB parser to read from and write into my xml file using java code. My xml file contains a DOCTYPE element pointing to a .dtd file which does not exist, due to which I get a FileNotFoundException when JAXB tries to read the xml file. Hence, after unmarshalling the xml file using jaxb, I use a SAXParser to get an XMLReader which removes this DOCTYPE element as given below -
    SAXParserFactory parserFactory = SAXParserFactory.newInstance();
    parserFactory.setValidating(false);
    XMLReader reader = parserFactory.newSAXParser().getXMLReader();
    reader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
    After making the required changes to the xml file and marshalling it back, I then use an XMLWriter to add the DOCTYPE element back to the xml file as given in the code snippet below -
    SAXReader saxReader = new SAXReader();
    Document document = saxReader.read(_file);
    document.addDocType(rootElement, publicUri, systemUri);
    XMLWriter output = new XMLWriter(new FileWriter( _file ));
    output.write( document );
    output.close();
    Right now I am using both, the JAXB parser to read from and write into the xml file and SAXParser/SAXRader just to delete/add the DOCTYPE element. I want to avoid using two different parsers in my class and want to know if JAXB provides any mechanism to delete and then add this element while parsing the xml file.

    Standard answer for this FAQ: set an EntityResolver on your parser that sends an empty string instead of the DTD. The API docs have an example that you could modify to do that.

  • How to create empty text box in wad

    hi
    can i get step by step process for create empty text box user need to enter some text after wad execution or i need keep default tex also?
    please let me know#

    Hi
    can you give me more clarification please?
    i have 1 report
    comp code material plant qty
    i am using variable as comp code
    if i give variable comp code as 1000
    i want get output and at report right side   i want see variable    comp code: 1000
    how can i ge this ple?
    regards
    suneel.

Maybe you are looking for