3

I am trying to parse through a XML format String for example;

<params  city="SANTA ANA" dateOfBirth="1970-01-01"/>

My goal is to add attributes name in an array list such as {city,dateOfBirth} and values of the attributes in another array list such as {Santa Ana, 1970-01-01} any advice, please help!

2
  • 4
    What exactly do you need help with? Commented Jun 25, 2015 at 23:36
  • you need use SAX parser Commented Jun 25, 2015 at 23:58

2 Answers 2

1
  1. Create SAXParserFactory.
  2. Create SAXParser.
  3. Create YourHandler, which extends DefaultHandler.
  4. Parse your file using SAXParser and YourHandler.

For example:

try {
    SAXParserFactory factory = SAXParserFactory.newInstance();
    SAXParser parser = factory.newSAXParser();
    parser.parse(yourFile, new YourHandler());
} catch (ParserConfigurationException e) {
    System.err.println(e.getMessage());
}

where, yourFile - object of the File class.

In YourHandler class:

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class YourHandler extends DefaultHandler {
    String tag = "params"; // needed tag
    String city = "city"; // name of the attribute
    String value; // your value of the city

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        if(localName.equals(tag)) {
            value = attributes.getValue(city);
        }
    }

    public String getValue() {
        return value;
    }
}`

More information for SAX parser and DefaultHandler here and here respectively.

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

Comments

1

Using JDOM (http://www.jdom.org/docs/apidocs/):

    String myString = "<params city='SANTA ANA' dateOfBirth='1970-01-01'/>";
    SAXBuilder builder = new SAXBuilder();
    Document myStringAsXML = builder.build(new StringReader(myString));
    Element rootElement = myStringAsXML.getRootElement();
    ArrayList<String> attributeNames = new ArrayList<String>();
    ArrayList<String> values = new ArrayList<String>();
    List<Attribute> attributes = new ArrayList<Attribute>();
    attributes.addAll(rootElement.getAttributes());
    Iterator<Element> childIterator = rootElement.getDescendants();

    while (childIterator.hasNext()) {
        Element childElement = childIterator.next();
        attributes.addAll(childElement.getAttributes());
    }

    for (Attribute attribute: attributes) {
        attributeNames.add(attribute.getName());
        values.add(attribute.getValue());
    }

    System.out.println("Attribute names: " + attributeNames); 
    System.out.println("Values: " + values); 

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.