如何使用从XSD到XML的元素执行不同的顺序

时间:2019-05-22 11:11:13

标签: java xml xsd

昨天,我问的是从CSV解析文件到XML的特定输出,但是今天我遇到了一个新问题。我从我们的合作伙伴那里得到了一个XSD模式,并生成了JAXB类。问题是元素在和之后执行,但应该在之前执行。我该如何解决?

我试图更改XSD的结构并重新生成JAXB类,但这没有帮助。

这是XSD出现问题的部分:

<xs:complexType name="Posiljka" abstract="true">
    <xs:sequence>
      <xs:element name="MAS" type="xs:decimal" />
      <xs:element name="DRZ" type="Drzava" />
      <xs:element minOccurs="0" name="GS" type="Storitev" />
      <xs:element minOccurs="0" name="ODK" type="xs:decimal" />
      <xs:element minOccurs="0" name="VRE" type="xs:decimal" />
      <xs:element minOccurs="0" name="VPD" type="xs:string" />
      <xs:element minOccurs="0" name="VPL" type="xs:decimal" />
      <xs:element minOccurs="0" name="VREDTS" type="xs:decimal" />
      <xs:element minOccurs="0" name="POS" type="xs:decimal" />
      <xs:element minOccurs="0" name="PZ" type="xs:decimal" />
      <xs:element minOccurs="0" name="PO" type="xs:decimal" />
      <xs:element minOccurs="0" name="PPS" type="xs:decimal" />
      <xs:element minOccurs="0" name="PPN" type="xs:decimal" />
      <xs:element minOccurs="0" name="DST" type="xs:string" />
      <xs:element minOccurs="0" name="DS" type="ArrayOfDodatneStoritve" />
    </xs:sequence>
  </xs:complexType>
  <xs:complexType name="RegistriranaPosiljka">
    <xs:complexContent mixed="false">
      <xs:extension base="Posiljka">
        <xs:sequence>
          <xs:element name="ZAPST" type="xs:int" />
          <xs:element minOccurs="0" name="NAS" type="Naslovnik" />
          <xs:element name="REG" type="xs:string" />
          <xs:element minOccurs="0" name="DODPOS" type="ArrayOfDODREGPOS" />
          <xs:element minOccurs="0" name="DP" type="DodatniPodatki" />
        </xs:sequence>
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>

这是代码的一部分,它提取要放入XML中的数据:


    String line = "";
        String cvsSplitBy = "|";

        Oddaja oddaja = new Oddaja();
        ArrayOfRegPosiljka regp = new ArrayOfRegPosiljka();

        oddaja.setWPID("CertSubject");
        oddaja.setKOMID(21553);
        oddaja.setSTODD(0);
        oddaja.setPOGID(2427);
        oddaja.setPODID(0);
        oddaja.setPOSID("7733");
        oddaja.setRegistriranePosiljke(regp);

        ObjectFactory fc = new ObjectFactory();
        Oddaja odd = fc.createOddaja();
        odd.setWPID(oddaja.getWPID());
        odd.setSTODD(oddaja.getSTODD());
        odd.setKOMID(oddaja.getKOMID());
        odd.setPOGID(oddaja.getPOGID());
        odd.setPODID(oddaja.getPODID());
        odd.setPOSID(oddaja.getPOSID());

        odd.setRegistriranePosiljke(oddaja.getRegistriranePosiljke());

        try (BufferedReader br = new BufferedReader(new FileReader(csvFile))) {

            while ((line = br.readLine()) != null) {

                // use delimiter as separator
                String[] podatki = line.split(Pattern.quote(cvsSplitBy));

                RegistriranaPosiljka regpos = new RegistriranaPosiljka();
                regpos.setZAPST(Integer.parseInt(podatki[0]));
                regpos.setREG(podatki[1]);

                Naslovnik nasl = new Naslovnik();
                nasl.setNAZ(podatki[2]);
                nasl.setPOSTST(podatki[3]);

                regpos.setMAS(new BigDecimal(podatki[4]));

                Storitev sto = new Storitev();
                sto.setNAZ(podatki[7]);

                regpos.setGS(sto);
                regpos.setNAS(nasl);

                regp.getREGPOS().add(regpos);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

        try{
            JAXBContext jc = JAXBContext.newInstance("generated");
            JAXBElement<Oddaja> element = fc.createOddaja(oddaja);
            Marshaller ms = jc.createMarshaller();
            ms.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            ms.marshal(element, System.out);
            ms.marshal(element, new File("src/eSpremnicaXML/test2.xml"));
        } catch(Exception e) {
            e.printStackTrace();
        }

    }
}```

0 个答案:

没有答案