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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java XML嵌套的Parser无法正常工作相关的知识,希望对你有一定的参考价值。

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

org.xml.sax.SAXParseException; lineNumber:1; columnNumber:1;序言中不能有内容。 at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:257)at 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)。

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

答案

例外

您得到的错误是由于您的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();

以上是关于Java XML嵌套的Parser无法正常工作的主要内容,如果未能解决你的问题,请参考以下文章

嵌套滚动视图的滚动无法正常工作

嵌套在 ngIf 中时,异步管道订阅无法正常工作

无法让 Rails 3 Active Scaffold 嵌套表单/记录正常工作

带有嵌套for循环的Javascript多维数组-无法正常工作

Django 从嵌套应用程序加载模板标签无法正常工作

使用 Array.map 时嵌套承诺,使用 Promise.all 但仍然无法正常工作