Java XML嵌套的Parser无法正常工作

时间:2018-02-27 17:39:09

标签: java xml saxparser

XML解析器无法正常工作,我收到以下错误:

  

org.xml.sax.SAXParseException; lineNumber:1; columnNumber:1;   序言中不能有内容。在   com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:257)     在   com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:339)     在openexcel.xmlreader.main(xmlreader.java:24)

这是我的XML文档

</service>
 </services>
 <solutions>
      <solution>
           <cost>505.9214670746815</cost>
           <routes>
                <route>
                     <driverId>noDriver</driverId>
                     <vehicleId>1_1</vehicleId>
                     <start>0.0</start>
                     <act type="service">
                          <serviceId>5 WassimKarim maudarbaccus</serviceId>
                          <arrTime>109.9819741964403</arrTime>
                          <endTime>119.9819741964403</endTime>
                     </act>
                     <end>229.9639483928806</end>
                </route>
                <route>
                     <driverId>noDriver</driverId>
                     <vehicleId>3_1</vehicleId>
                     <start>0.0</start>
                     <act type="service">
                          <serviceId>4 Jean Nicolas Yung</serviceId>
                          <arrTime>109.98190391287031</arrTime>
                          <endTime>119.98190391287031</endTime>
                     </act>
                     <act type="service">
                          <serviceId>2 George Bush</serviceId>
                          <arrTime>119.98282618841856</arrTime>
                          <endTime>129.98282618841856</endTime>
                     </act>
                     <act type="service">
                          <serviceId>3 Saddam Usain</serviceId>
                          <arrTime>129.98343325287408</arrTime>
                          <endTime>139.98343325287408</endTime>
                     </act>
                     <act type="service">
                          <serviceId>1 Barack OBama</serviceId>
                          <arrTime>139.98435552842233</arrTime>
                          <endTime>149.98435552842233</endTime>
                     </act>
                     <end>259.9673220629237</end>
                </route>
           </routes>
      </solution>
      <solution>
           <cost>505.9208295302417</cost>
           <routes>
                <route>
                     <driverId>noDriver</driverId>
                     <vehicleId>1_2</vehicleId>
                     <start>0.0</start>
                     <act type="service">
                          <serviceId>5 Oppa Gamnamstyle</serviceId>
                          <arrTime>109.9819741964403</arrTime>
                          <endTime>119.9819741964403</endTime>
                     </act>
                     <end>229.9639483928806</end>
                </route>
                <route>
                     <driverId>noDriver</driverId>
                     <vehicleId>3_1</vehicleId>
                     <start>0.0</start>
                     <act type="service">
                          <serviceId>4 Jean Nicolas Yung</serviceId>
                          <arrTime>109.98190391287031</arrTime>
                          <endTime>119.98190391287031</endTime>
                     </act>
                     <act type="service">
                          <serviceId>2 Emilie Sparks</serviceId>
                          <arrTime>119.98282618841856</arrTime>
                          <endTime>129.98282618841856</endTime>
                     </act>
                     <act type="service">
                          <serviceId>1 Denzel Wshington</serviceId>
                          <arrTime>129.98372097890456</arrTime>
                          <endTime>139.98372097890456</endTime>
                     </act>
                     <act type="service">
                          <serviceId>3 Mona Lisa</serviceId>
                          <arrTime>139.9846432544528</arrTime>
                          <endTime>149.9846432544528</endTime>
                     </act>
                     <end>259.9668316441239</end>
                </route>
           </routes>
      </solution>
 </solutions>
</problem>

我正在尝试为XML文件中的每个实体获取几个字段(cost,driverid,vehicleid,start,act,serviceId,arrTime)。

到目前为止我一直在尝试的是

1 个答案:

答案 0 :(得分:0)

例外

您获得的错误是由于您的XML格式不正确,XML的开头似乎不正确,您可以删除吗

</service>
</services>

并将其替换为

<problem>

这样你的XML就这样开始了:

<problem>
  <solutions>
    <solution>
      ..

这将匹配文件的结束标记

    </solution>
  </solutions>
</problem>

我尝试了它,现在可以解析了。

提取值

如上所述,获得有效的XML后。

我们说我们有以下课程

class Solution {
  private Double cost;
  private List<Route> routes = new ArrayList<>();
  // Getters/Setters
}

class Route {
  private String driverId;
  private String vehicleId;
  private Double start;
  private Double end;
  private Act act;
  // Getters/Setters
}

class Act {
  private String type;
  private String serviceId;
  private Double arrTime;
  private Double endTime;
  // Getters/Setters
}

你需要创建一个处理程序,在读取xml

时构建你的对象
class MyHandler extends DefaultHandler {

  // This will hold the result of the parsing
  private List<Solution> solutions = new ArrayList<>();

  // These are local variables used to create the objects
  private Solution solution;
  private Route route;
  private Act act;

  // The name of the element we are reading
  private String currentElementName = "";

  private static String SOLUTION_TAG = "solution";
  private static String ROUTE_TAG = "route";
  private static String ACT_TAG = "act";
  private static String COST_TAG = "cost";
  private static String DRIVERID_TAG = "driverId";
  private static String VEHICLEID_TAG = "vehicleId";
  private static String START_TAG = "start";
  private static String END_TAG = "end";
  private static String SERVICEID_TAG = "serviceId";
  private static String ARRTIME_TAG = "arrTime";
  private static String ENDTIME_TAG = "endTime";

  private static String ACT_TYPE_ATTRIBUTE = "type";

  public List<Solution> getSolutions() {
    return solutions;
  }

  // Called when we start a new element, that is where we create the objects
  public void startElement(String uri, String localName, String qName, Attributes attributes) {
    currentElementName = qName;
    if (qName.equalsIgnoreCase(SOLUTION_TAG)) {
      solution = new Solution();
    } else  if (qName.equalsIgnoreCase(ROUTE_TAG)) {
      route = new Route();
    } else  if (qName.equalsIgnoreCase(ACT_TAG)) {
      act = new Act();
      act.setType(attributes.getValue(ACT_TYPE_ATTRIBUTE));
    }
  }

  // Called when we ending an element, that is where we stick the objects one in an other
  public void endElement(String uri, String localName, String qName) {
    if (qName.equalsIgnoreCase(ACT_TAG)) {
      route.setAct(act);
    } else  if (qName.equalsIgnoreCase(ROUTE_TAG)) {
      solution.getRoutes().add(route);
    } else  if (qName.equalsIgnoreCase(SOLUTION_TAG)) {
      solutions.add(solution);
    }
    currentElementName = "";
  }

  // Called when we reading the text inside an element, that is where we set values to the object attributes
  public void characters(char ch[], int start, int length) {
    String s = new String(ch, start, length);
    if(currentElementName.equalsIgnoreCase(COST_TAG)) {
      solution.setCost(Double.valueOf(s));
    } else if(currentElementName.equalsIgnoreCase(DRIVERID_TAG)) {
      route.setDriverId(s);
    } else if(currentElementName.equalsIgnoreCase(VEHICLEID_TAG)) {
      route.setVehicleId(s);
    } else if(currentElementName.equalsIgnoreCase(START_TAG)) {
      route.setStart(Double.valueOf(s));
    } else if(currentElementName.equalsIgnoreCase(END_TAG)) {
      route.setEnd(Double.valueOf(s));
    } else if(currentElementName.equalsIgnoreCase(SERVICEID_TAG)) {
      act.setServiceId(s);
    } else if(currentElementName.equalsIgnoreCase(ARRTIME_TAG)) {
      act.setArrTime(Double.valueOf(s));
    } else if(currentElementName.equalsIgnoreCase(ENDTIME_TAG)) {
      act.setEndTime(Double.valueOf(s));
    }
  }
}

然后您可以通过这种方式简单地解析XML:

MyHandler myHandler = new MyHandler();
try {
  SAXParserFactory factory = SAXParserFactory.newInstance();
  SAXParser saxParser = factory.newSAXParser();
  saxParser.parse(new InputSource(new StringReader(xml)), myHandler);
} catch (Exception e) {
  e.printStackTrace();
}

并且您已将所有文件解析为处理程序中的解决方案列表,您可以通过这种方式获取

myHandler.getSolutions();