验证 List(Jaxb) 中的未编组条目

Verifying unmarshalled entries from List( Jaxb)

我有 4 个 XML 文件,其结构如下:

<?xml version="1.0"?>
<Registry>
    <Insitutions>
        <Insitiution>
            <name>A</name>
            <location>B</location>
        </Insitiution>
        <Insitiution>
            <name>C</name>
            <location>D</location>
        </Insitiution>
        <Insitiution>
            <name>E</name>
            <location>F</location>
        </Insitiution>
    </Insitutions>
</Registry>

我已经为单个 XML 标记编写了 类,并使用 jaxb 对它们进行了解组。 我解组所有文件并将所有条目存储在列表中,如下所示:

  private static List<String> loadBaseNodesets() throws JAXBException {
        log.info("Loading nodesets");
        Registry xmlentries = null;
        JAXBContext jaxbContext = null;
        List<Registry> entries = new ArrayList<>();
        List<String> privateBaseNodeSets= new ArrayList<>();
        File dir = new File("XMLFiles");
        if (dir.exists() && dir.isDirectory()) {
            FileFilter filter = new FileFilter() {
                public boolean accept(File file) {
                    return file.isFile() && file.getName().endsWith(".xml");
                }
            };
        log.info("iterating through all files");
        File [] files = dir.listFiles(filter);
        if (files != null) {                    
                for (int i =0;i <1;i++) {   
                    jaxbContext = JAXBContext.newInstance(Registry.class);
                    Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
                    xmlentries = (Registry) jaxbUnmarshaller.unmarshal(files[i]);
                    privateBaseNodeSets.add(xmlentries.toString());
                    log.info(files[i].getName()+" NodeSet loaded");
                    entries.add(xmlentries);            
                }
            }
        
        }       
        log.info("Nodeset loading complete");
    return privateBaseNodeSets;
  }

我需要将所有的 xmlentries 放入一个字符串列表中。

现在从主程序中,我想检查是否可以获得与 XML 条目相同的格式。

 public static void main(String[] args) throws JAXBException {
    log.info("Started local test main");
    List<String> baseNodesets = loadBaseNodesets();
    ListIterator<String> litr = baseNodesets.listIterator();
    while (litr.hasNext()) {
        
        JAXBContext jaxbContext=JAXBContext.newInstance(Registry.class);
        Marshaller marshaller = jaxbContext.createMarshaller();#
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(, System.out);// DONT KNOW THE FIRST PARAMETER
        }

我无法在此处获取第一个参数。有人可以帮忙吗?

您的方法 marshal 需要将 java 对象转换为 XML 作为第一个参数。

这会将您的条目列表转换为 xml.

的列表
    final JAXBContext jaxbContext=JAXBContext.newInstance(Registry.class);
    final Marshaller marshaller = jaxbContext.createMarshaller();
    List<String> xmlEntries = entries.stream().map(entrie -> {
      OutputStream os = new ByteArrayOutputStream();
      marshaller.marshal(entrie, os); <-- your object to transform to XML
      return os.toString();
    }.collect(Collectors.toList());

我不确定我是否正确理解了你的问题,但我猜你正试图做这样的事情:

  1. 一个一个地读取每个文件并解组并存储在entries列表中。
  2. 然后遍历 entries List 并一一编组。
  3. 将转换后的XML存入List

我在一个文件夹中存储了 2 个相同的 XML 文件并尝试读取它

XML 个文件:

<?xml version="1.0"?>
<Registry>
    <Insitutions>
        <Insitiution>
            <name>A</name>
            <location>B</location>
        </Insitiution>
        <Insitiution>
            <name>C</name>
            <location>D</location>
        </Insitiution>
        <Insitiution>
            <name>E</name>
            <location>F</location>
        </Insitiution>
    </Insitutions>
</Registry>

根目录:

@Data
@XmlRootElement(name = "Registry")
@XmlAccessorType(XmlAccessType.FIELD)
public class Root {
    @XmlElementWrapper(name = "Insitutions")
    @XmlElement(name = "Insitiution")
    private List<Insitiution> Insitiution;
}

机构:

@Data
@XmlAccessorType(XmlAccessType.FIELD)
public class Insitiution {
    private String name;
    private String location;
}

编组(主要):

public class Marshalling {

    public static void main(String[] args) throws JAXBException, XMLStreamException, FactoryConfigurationError, IOException, SAXException {
        final File dir = new File("/Users/Downloads/test");
        final List<Root> entries = new ArrayList<>();
        final Unmarshaller unmarshaller = JAXBContext.newInstance(com.newJAXB.Root.class).createUnmarshaller();
        final StringWriter singleXmlEvent = new StringWriter();
        final List<String> xmlOutput = new ArrayList<>();
        final Marshaller marshaller = JAXBContext.newInstance(Root.class).createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

        for (File file : dir.listFiles()) {
            if (!file.isDirectory() && file.getName().endsWith(".xml")) {
                //Storing all the files into entries List
                final DataInputStream inputStream = new DataInputStream(new FileInputStream(file.getAbsolutePath()));
                final XMLStreamReader xmlStreamReader = XMLInputFactory.newInstance().createXMLStreamReader(inputStream);
                final com.newJAXB.Root root = unmarshaller.unmarshal(xmlStreamReader, com.newJAXB.Root.class).getValue();
                entries.add(root);
                System.out.println(root.toString());
            }
        }

        //Loop through each entry in entries and marshal it
        for (Root item : entries) {
            marshaller.marshal(item, singleXmlEvent);
            xmlOutput.add(singleXmlEvent.toString());
            // Clear the StringWriter for next event
            singleXmlEvent.getBuffer().setLength(0);
        }

        //Final all the XML
        System.out.println(xmlOutput);
    }
}

输出:

Root(Insitiution=[Insitiution(name=A, location=B), Insitiution(name=C, location=D), Insitiution(name=E, location=F)])
Root(Insitiution=[Insitiution(name=A, location=B), Insitiution(name=C, location=D), Insitiution(name=E, location=F)])
[<?xml version="1.0" encoding="UTF-8"?>
<Registry>
   <Insitutions>
      <Insitiution>
         <name>A</name>
         <location>B</location>
      </Insitiution>
      <Insitiution>
         <name>C</name>
         <location>D</location>
      </Insitiution>
      <Insitiution>
         <name>E</name>
         <location>F</location>
      </Insitiution>
   </Insitutions>
</Registry>
, <?xml version="1.0" encoding="UTF-8"?>
<Registry>
   <Insitutions>
      <Insitiution>
         <name>A</name>
         <location>B</location>
      </Insitiution>
      <Insitiution>
         <name>C</name>
         <location>D</location>
      </Insitiution>
      <Insitiution>
         <name>E</name>
         <location>F</location>
      </Insitiution>
   </Insitutions>
</Registry>
]