【问题标题】:QuickfixJ create message from xml stringQuickfixJ 从 xml 字符串创建消息
【发布时间】:2018-07-31 21:05:39
【问题描述】:

QuickFixJ Message 类有方法 toXML() 将消息转换为 xml 字符串。 有什么方法可以从 XML 字符串创建消息对象? 我需要 toXML() 的反向,即我想从 xml 创建消息。

【问题讨论】:

    标签: quickfix fix-protocol quickfixj


    【解决方案1】:

    没有类似的内置功能。实际上不需要,因为通常不会有它的用例。

    我已经写了一个类来做到这一点。标签的顺序可能与输入消息不同(但 FIX 规范不保证标签顺序,组内除外),因为 XML 导出器按标签编号排序,因此原始标签顺序丢失。

    它仅适用于 XML 文件中的单个消息,但可以轻松适应多个消息。

    您可以使用标准MessageUtils.parse 从结果字符串创建Message

    如果您有任何问题,请告诉我。

    class XmlMessage
    {
        private final String xml;
        private final String delimiter;
    
        XmlMessage(final String xml, final String delimiter)
        {
            this.xml = xml;
            this.delimiter = delimiter;
        }
    
        public String toFixMessage() throws IOException, SAXException, ParserConfigurationException
        {
            final Document doc = DocumentBuilderFactory.newInstance()
                .newDocumentBuilder()
                .parse(new ByteArrayInputStream(xml.getBytes()));
    
            final StringBuilder messageBuilder = new StringBuilder();
            build(messageBuilder, doc, "header");
            build(messageBuilder, doc, "body");
            build(messageBuilder, doc, "trailer");
            return messageBuilder.toString();
        }
    
        private void build(final StringBuilder messageBuilder, final Document doc, final String section)
        {
            final NodeList sectionRoot = doc.getElementsByTagName(section);
            final NodeList sectionChildren = sectionRoot.item(0).getChildNodes();
            build(messageBuilder, sectionChildren);
        }
    
        private void build(final StringBuilder messageBuilder, final NodeList nodeList)
        {
            final Set<String> numInGroupTags = getNumInGroupTags(nodeList);
            for (int i = 0; i < nodeList.getLength(); i++)
            {
                final Node node = nodeList.item(i);
                if (node.getNodeName().equals("field") && !numInGroupTags.contains(getTagNumber(node)))
                {
                    messageBuilder.append(getTagNumber(node))
                        .append('=')
                        .append(node.getTextContent())
                        .append(delimiter);
                }
                else if (node.getNodeName().equals("groups"))
                {
                    final NodeList groupElems = node.getChildNodes();
                    messageBuilder.append(getTagNumber(node))
                        .append('=')
                        .append(getGroupCount(groupElems))
                        .append(delimiter);
                    for (int j = 0; j < groupElems.getLength(); j++)
                    {
                        build(messageBuilder, groupElems.item(j).getChildNodes());
                    }
                }
            }
        }
    
        private Set<String> getNumInGroupTags(final NodeList nodeList)
        {
            final Set<String> numInGroupTags = new HashSet<>();
            for (int i = 0; i < nodeList.getLength(); i++)
            {
                if (nodeList.item(i).getNodeName().equals("groups"))
                {
                    numInGroupTags.add(getTagNumber(nodeList.item(i)));
                }
            }
            return numInGroupTags;
        }
    
        private String getTagNumber(final Node node)
        {
            return node.getAttributes().getNamedItem("tag").getTextContent();
        }
    
        private int getGroupCount(final NodeList groupRoot)
        {
            int count = 0;
            for (int j = 0; j < groupRoot.getLength(); j++)
            {
                if (groupRoot.item(j).getNodeName().equals("group")) count++;
            }
            return count;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-07
      • 2017-02-03
      • 2015-05-28
      • 2016-03-04
      • 1970-01-01
      • 1970-01-01
      • 2015-09-30
      • 1970-01-01
      相关资源
      最近更新 更多