Java xml解析 - 定位特定结果:

时间:2022-10-09 23:21:55

So I've been playing around with Sax the past few days and I've been completely unable to handle this issue I've run into.

所以过去几天我一直在和萨克斯一起玩,而且我已经完全无法处理这个问题了。

Assume that I have the following xml

假设我有以下xml

<PowerSystemRegions>
<PowerSystemRegion>
    <RegionId>1</RegionId>
    <RegionName>New England</RegionName>
    <Status>Normal</Status>
    <DateUpdated>2011-12-22T10:55:51.000-05:00</DateUpdated>
</PowerSystemRegion>
<PowerSystemRegion>

And I wanted to first check to make sure that the region name is New England, and then pull the status based off of that, but I don't think it would load the next qName or anything since it would call again from main.

我想首先检查以确保区域名称是新英格兰,然后基于此拉出状态,但我不认为它会加载下一个qName或任何东西,因为它将再次从main调用。

Here's what I have so far, but I'm utterly stuck:

这是我到目前为止所拥有的,但我完全陷入困境:

public void startElement (String uri, String name, String qName, Attributes atts) {
    boolean region = false;
    boolean regionName = false;
    boolean regionStatus = false;

    if (qName.compareTo("RegionName") == 0) {
        region = true;
        if(atts.getValue(0).compareTo("New England") == 0){

        }

        if (currentStatus == null){
        String cCond = atts.getValue(0);
        currentStatus = cCond;
        }   
    }
}

It was my idea to compare the names and once all 3 booleans were set I'd be fine... but then it struck me that it wouldn't work that way due to only having one qName in the string... Halp?

我的想法是比较这些名字,一旦所有3个布尔都设置好了我就没事了......但是后来我觉得由于字符串中只有一个qName而无法正常工作...... Halp?

1 个解决方案

#1


0  

It's been a while since I worked with sax, but I'll try: You could add in your handler fields like:

自从我使用sax以来已经有一段时间了,但我会尝试:你可以添加你的处理程序字段,如:

public class SaxHandler {
    Integer regionId;
    String regionName;
    String regionStatus;

    (...)
}

So, in your start element, you make something like:

所以,在你的start元素中,你做了类似的事情:

public void startElement (String uri, String name, String qName, Attributes atts) {
    if (qname.equals("RegionName") {
        regionName = atts.getValue(0);
    }
    if (qname.equals("RegionId") {
        regionId = Integer.parseInt(atts.getValue(0));
    }
    if (qname.equals ("Status") {
        if(regionName.equals ("New England")) {
            //Do what you want here
        }

    }

}

By the way, its better use equals instead of compareTo. Use compareTo only if you're sorting elements ;)

顺便说一句,它更好的使用等于而不是compareTo。只有在对元素进行排序时才使用compareTo;)

EDIT: Thinking in objects, better use classes, instead of fields like I did. Use a PowerSystem object to collect the data from the xml ;)

编辑:思考对象,更好地使用类,而不是像我一样的字段。使用PowerSystem对象从xml收集数据;)

#1


0  

It's been a while since I worked with sax, but I'll try: You could add in your handler fields like:

自从我使用sax以来已经有一段时间了,但我会尝试:你可以添加你的处理程序字段,如:

public class SaxHandler {
    Integer regionId;
    String regionName;
    String regionStatus;

    (...)
}

So, in your start element, you make something like:

所以,在你的start元素中,你做了类似的事情:

public void startElement (String uri, String name, String qName, Attributes atts) {
    if (qname.equals("RegionName") {
        regionName = atts.getValue(0);
    }
    if (qname.equals("RegionId") {
        regionId = Integer.parseInt(atts.getValue(0));
    }
    if (qname.equals ("Status") {
        if(regionName.equals ("New England")) {
            //Do what you want here
        }

    }

}

By the way, its better use equals instead of compareTo. Use compareTo only if you're sorting elements ;)

顺便说一句,它更好的使用等于而不是compareTo。只有在对元素进行排序时才使用compareTo;)

EDIT: Thinking in objects, better use classes, instead of fields like I did. Use a PowerSystem object to collect the data from the xml ;)

编辑:思考对象,更好地使用类,而不是像我一样的字段。使用PowerSystem对象从xml收集数据;)