Search code examples
xmldomxml-parsingsaxstax

why Attribute count is 0 for this XML Tag


I am using STAX Parser for parsing the XML documents . I have this below tag

<bustxml><![CDATA[&lt;bustxml xmlns=\"http://www.bustprotocol.org/bustxml-5-0-SP2\"&gt;&lt;NewOrdMBag TmInForce=\"0\" OrdTyp=\"1\" Acct=\"1234\"&gt;&lt;Ord OrdQty=\"1\" </bustxml>

I need to read the attributes of the above tag , so i used

 case XMLStreamConstants.START_ELEMENT:
                 for(int i = 0, n = reader.getAttributeCount(); i < n; ++i)
                  System.out.println("Attribute: " + reader.getAttributeName(i) 
                             + "" + reader.getAttributeValue(i));

But unfortunately i am getting , Attrbute Count as 0 . Please tell me how can i read all the contents inside the bustxml tag


Solution

  • The bustxml element outside the CDATA has no attributes.

    The bustxml inside the CDATA is not a tag. That's what CDATA means: "This is character data. Don't treat anything you find in here as markup." So the content might look like a tag, but it's not a tag, because of the CDATA, and therefore it has no attributes.

    Unfortunately it's very common for people to take XML and wrap inside CDATA. They do this to make your life difficult. (Well I assume that's the reason, I can't think of any other.) When this happens your only remedy is to extract the text inside the CDATA and submit it to an XML (or HTML) parser to turn it into a tree, and then you can access the elements and attributes in the normal way.

    In your case they've made it doubly difficult by not only wrapping it in CDATA, but escaping it as well. So they've effectively double-escaped it, so you're going to have to put it through the parser twice (in addition to the orginal parse) to make sense of it.

    I would complain very loudly to the people who sent you this rubbish.