SAX XML Parser - more than 2 attributes in a XML field - how to print

40 views Asked by At

I have an XML field in a file with more than 1 attribute. How do I print out both. My following code only prints one.. XML file : 0.52

Code:

public void startElement(String nameSpaceURI, 
     String localName, 
     String qName, 
     Attributes atts) {

    int attributeLength = atts.getLength();

    for (int i = 0; i < attributeLength; i++) {
        String attrName = atts.getLocalName(i);
        String attrVal = atts.getValue(i);  
    }

    System.out.print("<" + qName + attrName + attrVal + ">");
}

Thanks for your help in advance !!

1

There are 1 answers

0
MallSing On

It will be compiled since it will have atleast one attribute value. However, I found the simple solution that I was looking for..

public void startElement(String nameSpaceURI, String localName, String qName, Attributes atts) {

    StringBuilder attribute = new StringBuilder("");

    int attributeLength = atts.getLength();

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

            String attrName = atts.getLocalName(i);
            String attrVal = atts.getValue(i);
            attribute.append(" " + attrName + "=" + "\"" + attrVal + "\"");

        }

        System.out.print("<" + qName + attribute + ">");
}