0

I have one xml file to process, but the xml file is not in conventional xml format, normally xml have the following format, then i can use java's SAXParser to extract the info:

<Info>
<Product id>123456</Product id>
<code2>985632</code2>
<code3>896523</code3>
<Product id>123343</Product id>
<code2>935632</code2>
<code3>856523</code3>
</Info>

But now my xml is in this form, i cant use the SAXParser technique to search for start-tag, and end-tag. Any idea please?

<Info>
<Product id="123456" code2="985632" code3="896523" />
<Product id="123343" code2="935632" code3="856523" />
...
</Info>

Normally java SAX parser use the following methods to detect xml's start tag, xml's eng tag, and xml's content, but since my xml dont even have proper end tag, i not sure whether i can use java SAX parser or not.

public void startElement(String uri, String localName,
        String qName, Attributes attributes)
throws SAXException {

}

public void endElement(String uri, String localName, String qName)
throws SAXException {
}

public void characters(char ch[], int start, int length)
throws SAXException {
}
3
  • Are you getting any errors? Can you show us some code? Commented Dec 1, 2014 at 7:59
  • Is not error actually, but just cant use normal java SAX Parser to process the file since my xml file is not like conventional xml file Commented Dec 1, 2014 at 8:05
  • 1
    I think you can. You just have to get the attribute(s) instead of the value between the tags. Commented Dec 1, 2014 at 8:06

1 Answer 1

1

You will have to get the attributes of those tags by doing something like this:

@Override
public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {

    int length = attributes.getLength();

    for(int i=0; i<length; i++) {

            // Qualified name by index
            String name = attributes.getQName(i);

            // Attribute value by index
            String value = attributes.getValue(i);

            // Namespace URI by index
            String nsUri = attributes.getURI(i);

            // Local name by index
            String lName = attributes.getLocalName(i);
        }
    }

This will get all the attributes in the tag by the index.

Source

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.