使用JDOM在Java中构建包含我的FIX订单的.XML文件

时间:2014-03-29 15:36:42

标签: java xml jdom-2

我写了一个简单的方法来发送FIX命令。

我有一点问题,第一个存储的ClOrdID是1,接下来是11,然后是21,依此类推。我希望第一个元素为0或1,然后是2,3等......

我的代码出了什么问题?

private static void sendSELLOrderSingle(String account,
        String symbol, String securityID, String securityExchange,
        double price, double orderQty) throws SessionNotFound,
        JDOMException, IOException {

    quickfix.fix42.NewOrderSingle order = new quickfix.fix42.NewOrderSingle();

    SessionID sessionId = (SessionID) initiator.getSessions().get(0);



    order.set(new TransactTime(new Date(0)));
    order.set(new HandlInst('1'));

    order.set(new Account(account));
    order.set(new Symbol(symbol));
    order.set(new SecurityID(securityID));
    order.set(new SecurityExchange(securityExchange));
    order.set(new Price(price));
    order.set(new OrdType(OrdType.LIMIT));
    order.set(new Side(Side.SELL));
    order.set(new OrderQty(orderQty));



    // POST TRADE ACTIVITY

    org.jdom2.Document doc = new org.jdom2.Document();

    FileInputStream fis = new FileInputStream("XML/historical orders.xml");
    // create a sax builder to parse the document
    SAXBuilder sb = new SAXBuilder();
    // parse the xml content provided by the file input stream and create a
    // Document object
    doc = sb.build(fis);
    // get the root element of the document

    org.jdom2.Element root = doc.getRootElement();
    fis.close();

    org.jdom2.Element clOrdID1 = new org.jdom2.Element("ClOrdID");
    clOrdID1.setText((Integer.toString(root.getChildren().size())) + 1);
    root.addContent(clOrdID1);

    order.set(new ClOrdID((Integer.toString(root.getChildren().size())) + 1));

    org.jdom2.Element account1 = new org.jdom2.Element("Account");
    account1.setText(account);
    clOrdID1.addContent(account1);

    org.jdom2.Element symbol1 = new org.jdom2.Element("Symbol");
    symbol1.setText(symbol);
    clOrdID1.addContent(symbol1);

    org.jdom2.Element securityID1 = new org.jdom2.Element("SecurityID");
    securityID1.setText(securityID);
    clOrdID1.addContent(securityID1);

    org.jdom2.Element securityExchange1 = new org.jdom2.Element(
            "SecurityExchange");
    securityExchange1.setText(securityExchange);
    clOrdID1.addContent(securityExchange1);

    org.jdom2.Element price1 = new org.jdom2.Element("Price");
    price1.setText(Double.toString(price));
    clOrdID1.addContent(price1);

    org.jdom2.Element ordType = new org.jdom2.Element("Order_Type");
    ordType.setText("Limit");
    clOrdID1.addContent(ordType);

    org.jdom2.Element side = new org.jdom2.Element("Side");
    side.setText("SELL");
    clOrdID1.addContent(side);

    org.jdom2.Element ordQnty = new org.jdom2.Element("Order_Quantity");
    ordQnty.setText(Double.toString(orderQty));
    clOrdID1.addContent(ordQnty);

    try {
        // get object to see output of prepared document
        XMLOutputter xmlOutput = new XMLOutputter();

        // passsed System.out to see document content on console
        xmlOutput.output(doc, System.out);

        // passed fileWriter to write content in specified file

        xmlOutput.output(doc, new FileWriter("XML/historical orders.xml"));

    } catch (IOException io) {
        System.out.println(io.getMessage());
    }


    Session.sendToTarget(order, sessionId);
}

我的xml文件: The xml file

1 个答案:

答案 0 :(得分:1)

clOrdID1.setText((Integer.toString(root.getChildren().size())) + 1);

以上代码将获取元素数量的大小(例如1),然后将其转换为字符串,现在为"1",然后它将执行"1" + 1 },这是字符串连接,因此您将获得"11"

你想要做的是将+1移到toString()方法中,并使其添加而不是String-concatenation:

clOrdID1.setText((Integer.toString(1 + root.getChildren().size())));

排序!