我需要这段 Java 代码来发送目录中的所有 XML 文件,有没有办法遍历每个 XML 文件

I need this piece of Java code to send all the XML files in the directory, is there a way to loop through each XML file

我有一段 Java 可靠的代码,它从数据库中提取前 5 个 ID,并在 XML 文件中使用它创建 5 XML 个文件。

然后我从这个目录中单独挑选每个 xml 文件并将其发送到服务器,但我需要一个一个地发送它们。

public class sendPlaylistAcknowledgment {

Properties prop = new Properties(); // creating prop object as global variable

@BeforeTest

public void getData() throws IOException{

    FileInputStream f = new FileInputStream("D:\Tools\Workspace\BXF\src\files\config.properties"); //creating f object
    prop.load(f); // to load the file object into prop file

    //In the code below we will use the config values set in .property file
}

@SuppressWarnings("unused")
@Test
public void postData() throws IOException{


    String postData = GenerateStringFromResource("C:\Users\shussain\Desktop\BXF_Msg\PlaylistAck\BXF_1501110072083.XML");
    //BaseURL

    RestAssured.baseURI= prop.getProperty("AISHOST"); //value populating from property methord above

    Response resp = given().log().all().
    header("Content-Type", "application/XML; charset=utf-8").
    body(postData).
    when().
    post("/bxfxml").
    then().assertThat().statusCode(200).and().contentType(ContentType.XML).
    extract().response();

    //to convert raw data to string

    XmlPath xmlResponse= reusableFunctions.rawToXML(resp);
    String responseString = resp.asString();
    System.out.println("XML response is - "+ responseString);

}
 public static String GenerateStringFromResource(String path) throws IOException{

     return new String(Files.readAllBytes(Paths.get(path)));
 }

}
//Below code pulls data from DB and creates the XML file
public List <String> getExternalId() throws ClassNotFoundException, SQLException, InvalidFileFormatException, IOException{

    FileInputStream file = new FileInputStream("D:\Tools\Workspace\BXF\src\files\config.properties");
    Properties prop = new Properties();
    prop.load(file);

    Wini ini = new Wini(new File(prop.getProperty("WOINI")));

    String userid = ini.fetch("ConnectionString", "User ID");
    String pwd = ini.fetch("ConnectionString", "Password");
    String dbName = ini.fetch("ConnectionString", "Initial Catalog");
    String connectionURL = "jdbc:sqlserver://localhost" + ";databaseName="+dbName + ";user=" +userid + ";password=" +pwd;


    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

    Connection con = DriverManager.getConnection(connectionURL);
    System.out.println("Driver version: " + con.getMetaData().getDriverVersion());

    //retrieve data
    Statement st = con.createStatement();
    ResultSet rs = st.executeQuery(prop.getProperty("SELECT TOP 5 * FROM WO_INTERFACE_QUEUE WHERE ACTION_INFO = 'Playlist' ORDER BY CREATE_DATE DESC"));

    List <String> externalId = new ArrayList<String>();

    while (rs.next()) {
        externalId.add(rs.getString("INTERFACE_QUEUE_ID"));

        }

    return externalId;

}
   public void createBXFAckFXml(String id) {

          try {
             FileInputStream file = new FileInputStream("D:\Tools\Workspace\BXF\src\files\config.properties");
             Properties prop = new Properties();
             prop.load(file);

             DocumentBuilderFactory dbFactory =
             DocumentBuilderFactory.newInstance();
             DocumentBuilder dBuilder = 
                dbFactory.newDocumentBuilder();
             Document doc = dBuilder.newDocument();
             // root element
             Element rootElement = doc.createElement("BxfMessage");
             doc.appendChild(rootElement);

             // setting attribute to element
             rootElement.setAttribute("xmlns:xsd", "http://www.w3.org/2001/XMLSchema");
             rootElement.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
             rootElement.setAttribute("xmlns:xs", "http://www.w3.org/2001/XMLSchema");
             rootElement.setAttribute("id", "urn:uuid:427c5fb4-987c-4718-8ace-12fcb835df21");
             rootElement.setAttribute("dateTime", "2017-10-25T09:28:12.1074289-04:00");
             rootElement.setAttribute("messageType", "Acknowledgement");
             rootElement.setAttribute("origin", "ADC");
             rootElement.setAttribute("originType", "Automation System");
             rootElement.setAttribute("userName", "ADC_User");
             rootElement.setAttribute("destination", "WideOrbit");
             rootElement.setAttribute("originMessageId", id);
             rootElement.setAttribute("status", "OK");
             rootElement.setAttribute("ext:usage", "Application Acknowledgement");
             rootElement.setAttribute("xsi:schemaLocation", "http://smpte-ra.org/schemas/2021/2008/BXF bxfschema.xsd http://smpte-ra.org/schemas/2021/2008/BXF/Extension bxfschema-extension.xsd http://www.atsc.org/XMLSchemas/pmcp/2007/3.1 pmcp31.xsd");
             rootElement.setAttribute("xmlns:ext", "http://smpte-ra.org/schemas/2021/2008/BXF/Extension");
             rootElement.setAttribute("xmlns", "http://smpte-ra.org/schemas/2021/2008/BXF");

             // write the content into xml file
             TransformerFactory transformerFactory =
             TransformerFactory.newInstance();
             Transformer transformer =
             transformerFactory.newTransformer();
             DOMSource source = new DOMSource(doc);
             doc.setXmlStandalone(true);
             String filename = prop.getProperty("PLAYLISTACK_ENDPOINT")+ "BXF_"+System.currentTimeMillis()+".XML";
             StreamResult result =
             new StreamResult(new File(filename));
             transformer.transform(source, result);
             // Output to console for testing
             StreamResult consoleResult =
             new StreamResult(System.out);
             transformer.transform(source, consoleResult);
          } catch (Exception e) {
             e.printStackTrace();
          } 

在 xml 文件创建程序的下一个 运行 中,您可以将先前发送的数据的扩展名更改为其他内容,然后您可以循环遍历 xml 文件并可以发送新的数据

   Files.newDirectoryStream(Paths.get("."),
        path -> path.toString().endsWith(".xml"))
        .forEach(data -> {
          // send you data here
      });