Вопрос

I have .xml files inside a package in my Java project that contains data in the following format...

<?xml version="1.0"?>
<postcodes>
    <entry postcode='AB1 0AA' latitude='7.101478' longitude='2.242852' />
</postcodes>

I currently have overrided the startElement() in my custom DefaultHandler to the following;

public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
    if (attributes.getValue("postcode") == "AB43 8TZ"){
        System.out.println("The postcode 'AB43 8TZ', has a latitude of "+attributes.getValue("latitude")+" and a longitude of "+attributes.getValue("longitude"));
    }
}

I know the code is working outside of this method because I previously tested it by having it print out all of the attributes for each element and that worked fine. Now however, it does nothing, as if it never found that postcode value. (I know it's there because it's a copy paste job from the XML source)

Extra details; Apologies for originally leaving out important details. Some of these files have up to 50k lines, so storing them in memory is a no no if at all possible. As such, I am using SAX. As a side, I use the words "from these files from within my project" because I also can't find how to reference a file from within the same project rather than from an absolute directory.

Это было полезно?

Решение

(From comments as requested by OP.)

First, you cannot compare strings with the == operator. Use equals() instead. See the question How do I compare strings in Java? for more information.

Second, not every element has the postcode attribute, so it is possible that you will be invoking equals() on a null object, leading to NullPointerException. Do it the other way around, e.g.

"AB43 8TZ".equals(attributes.getValue("postcode"))

Другие советы

You would use an XML parser. Luckily, JDK offers these out-of-the-box in form of JAXP. Now, there are several ways to do it, as there are few major "flavours" of parsing XML. For this task, I believe DOM parser would be easiest to use. You could do it like that:

DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = builderFactory.newDocumentBuilder();
Document document = builder.parse(new File("name/of/the/file.xml"));
Element root = document.getDocumentElement();

and then use DOM traversal API.

Edit: it was not clear from the original question that the data you want to process is large. In that case, DOM parser is indeed not a good solution, precisely due to memory consumption. For the purpose of parsing large XML documents, SAX and StAX parsers were invented. You might find them a little more cumbersome to use, due to their streaming nature, but that's also the source of their efficiency. Linked Oracle JAXP tutorial has sections on SAX and StAX as well.

Assuming you can read the XML relatively quickly using SAX or DOM, I would parse it in advance, and use the attributes to construct a map of postcode vs long/lang e.g.

Map<String, Pair<BigDecimal,BigDecimal>>

and simply lookup using Map.get(String)

I note that you say:

Some of these files have up to 50k lines, so storing them in memory is a no no if at all possible

I wouldn't worry about that at all. A map of 50k entries isn't going to be a major deal.

You can use the javax.xml.xpath APIs included in the JDK/JRE and use XPath to specify the data you wish to retrieve from the XML document.

Example

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top