Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/385.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将自定义字符串转换为xml_Java - Fatal编程技术网

Java 将自定义字符串转换为xml

Java 将自定义字符串转换为xml,java,Java,需要将自定义字符串格式转换为xml帮助。Java和C# [Node(X)][CHILD0(Y)][OBJECT1(A)][Key1(1)][Key2(2)] [Node(X)][CHILD0(Y)][OBJECT1(B)][Key1(1)][Key2(2)][Key3(3)] [Node(X)][CHILD0(Y)][OBJECT1(C)][Key1(4)] [Node(X)][CHILD0(Y)][OBJECT2(A)][Key1(1)][Key2(2)][Key3(3)] [Node(X)]

需要将自定义字符串格式转换为xml帮助。Java和C#

[Node(X)][CHILD0(Y)][OBJECT1(A)][Key1(1)][Key2(2)]

[Node(X)][CHILD0(Y)][OBJECT1(B)][Key1(1)][Key2(2)][Key3(3)]

[Node(X)][CHILD0(Y)][OBJECT1(C)][Key1(4)]

[Node(X)][CHILD0(Y)][OBJECT2(A)][Key1(1)][Key2(2)][Key3(3)]

[Node(X)][CHILD0(Y)][OBJECT2(B)][Key1(4)][Key2(5)]

[Node(X)][CHILD1(Z)][OBJECT1(A)][Key1(7)][Key2(8)][Key3(9)]

[Node(X)][CHILD1(Z)][OBJECT2(A)][Key1(15)][Key2(18)]

与上述示例类似,可能有“n”个字符串行。 将其序列化为xml文件的最佳方法是什么,如下所示

如果下面显示的xml也不正确,请告知正确的格式。 我尝试使用stackoverflow和CodeProject中提供的一些示例进行序列化,但无法实现 获取以下格式的xml。试着用java和c来做。提前谢谢大家

[X]
    [Y]  
        [OBJECT1]
            [A]
                <Key1>1</Key1>
                <Key2>2</Key2>
            [/A]
            [B]
            <Key1>2</Key1>
            <Key2>2</Key2>
            <Key3>3</Key3>
        [/B]
        [C]
            <Key1>4</Key1>
        [/C]
    [/OBJECT1]
    [OBJECT2]
        [A]
            <Key1>1</Key1>
            <Key2>2</Key2>
            <Key3>3</Key3>
        [/A]
        [B]
            <Key1>4</Key1>
            <Key2>5</Key2>
        [/B]
    [/OBJECT2]
    [/Y]
    [Z]
        [OBJECT1]
        [A]
            <Key1>7</Key1>
            <Key2>8</Key2>
            <Key2>9</Key2>
            [/A]
        [/OBJECT1]
        [OBJECT2]
        [A]
            <Key1>15</Key1>
            <Key2>18</Key2>
        [/A]
        [/OBJECT2]
    [/Z]
[/X]
[X]
[Y]
[反对意见1]
[A]
1.
2.
[A]
[乙]
2.
2.
3.
[B]
[丙]
4.
[C]
[/OBJECT1]
[反对意见2]
[A]
1.
2.
3.
[A]
[乙]
4.
5.
[B]
[/OBJECT2]
[Y]
[Z]
[反对意见1]
[A]
7.
8.
9
[A]
[/OBJECT1]
[反对意见2]
[A]
15
18
[A]
[/OBJECT2]
[Z]
[/X]

这里有一个解析器类供您使用:

public class XmlSerializer {

private String input;
private Element currentNode, currentChild, currentObject;
private Map<String, Element> nodes;
private Map<Element, Map<String, Element>> children, objects;

/**
 * Initializes the serializer with the given input string.
 * @param  input input string
 */
public XmlSerializer(String input) {
    this.input = input;
    this.nodes = new HashMap<String, Element>();
    this.children = new HashMap<Element, Map<String,Element>>();
    this.objects = new HashMap<Element, Map<String,Element>>();
}

/**
 * Parses the input string and returns the XML document.
 * @return XML document
 */
public Document parseDocument()
throws ParserConfigurationException {
    Pattern pattern = Pattern.compile("\\[(.+?)\\((.+?)\\)\\]");
    Matcher matcher = pattern.matcher(input);
    Document dom = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
    Element root = dom.createElement("root");
    dom.appendChild(root);

    while (matcher.find()) {
        String key = matcher.group(1);
        String value = matcher.group(2);

        if ("Node".equals(key)) {
            currentNode = parseElement(key, value, dom, root, nodes, children);
        } else if (currentNode != null && key.startsWith("CHILD")) {
            currentChild = parseElement(key, value, dom, currentNode,
                    children.get(currentNode), objects);
        } else if (currentChild != null && key.startsWith("OBJECT")) {
            currentObject = parseElement(key, value, dom, currentChild,
                    objects.get(currentChild), null);
        } else {
            Element property = parseProperty(key, value, dom);
            currentObject.appendChild(property);
        }
    }

    return dom;
}

/**
 * Returns the parsed XML document as string.
 * @return XML document as string
 */
public String toXML()
throws TransformerFactoryConfigurationError, ParserConfigurationException, TransformerException {
    StringWriter writer = new StringWriter();
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
    Source source = new DOMSource(parseDocument());
    Result result = new StreamResult(writer);
    transformer.transform(source, result);
    return writer.getBuffer().toString();
}

/**
 * Parses an element.
 * @param  key      key in {@code [key(value)]} string
 * @param  value    value in {@code [key(value)]} string
 * @param  dom      DOM
 * @param  parent   parent element to add this element to
 * @param  cache    cache for the parsed element
 * @param  subCache sub-cache to initialize (optional)
 * @return the element
 */
private Element parseElement(String key, String value, Document dom, Element parent,
        Map<String, Element> cache, Map<Element, Map<String, Element>> subCache) {
    Element el = cache.get(value);
    if (el == null) {
        el = dom.createElement(value);
        cache.put(key, el);
        parent.appendChild(el);
        if (subCache != null)
            subCache.put(el, new HashMap<String, Element>());
    }
    return el;
}

/**
 * Parses a property element.
 * @param  key   key in {@code [key(value)]} string
 * @param  value value in {@code [key(value)]} string
 * @param  dom   DOM
 * @return the element
 */
private Element parseProperty(String key, String value, Document dom) {
    Element property = dom.createElement(key);
    property.setTextContent(value);
    return property;
}

}
输出:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<root>
  <X>
    <Y>
      <A>
        <Key1>1</Key1>
        <Key2>2</Key2>
      </A>
    </Y>
    <Y>
      <B>
        <Key1>1</Key1>
        <Key2>2</Key2>
        <Key3>3</Key3>
      </B>
    </Y>
  </X>
  <X>
    <Y>
      <C>
        <Key1>4</Key1>
      </C>
    </Y>
  </X>
  <X>
    <Y>
      <A>
        <Key1>1</Key1>
        <Key2>2</Key2>
        <Key3>3</Key3>
      </A>
    </Y>
  </X>
  <X>
    <Y>
      <B>
         <Key1>4</Key1>
         <Key2>5</Key2>
      </B>
    </Y>
  </X>
  <X>
    <Z>
      <A>
        <Key1>7</Key1>
        <Key2>8</Key2>
        <Key3>9</Key3>
      </A>
    </Z>
  </X>
  <X>
    <Z>
      <A>
        <Key1>15</Key1>
        <Key2>18</Key2>
      </A>
    </Z>
  </X>
</root>

1.
2.
1.
2.
3.
4.
1.
2.
3.
4.
5.
7.
8.
9
15
18

从这里开始,对其进行一些优化,例如,如果您不希望重复
节点。

这看起来不像XML,那么您的代码在哪里?你到底有什么问题?Java或C#-选择一个,它们是不相关的。@Mat-如果我的问题让人困惑,很抱歉。我有n个自定义格式的字符串。[关键(价值)][关键(价值)]。。。具有可变数量的[键(值)]对。我想将其序列化为xml文件。正如我在第一篇文章中提到的,如果xml格式不正确,请忽略它,并建议实现这一点的最佳方法。谢谢。哎呀,忘了提了。这是关于Java的。谢谢。到目前为止你的密码在哪里?你被困在哪里?(有数以百万计的文档描述Web上的XML内容,还有大量其他文档介绍如何在Java中操作XML。)因此,如果输入是:
[Node(x)]
输出应该是:
?非常感谢jabu。感谢你的时间和努力。
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<root>
  <X>
    <Y>
      <A>
        <Key1>1</Key1>
        <Key2>2</Key2>
      </A>
    </Y>
    <Y>
      <B>
        <Key1>1</Key1>
        <Key2>2</Key2>
        <Key3>3</Key3>
      </B>
    </Y>
  </X>
  <X>
    <Y>
      <C>
        <Key1>4</Key1>
      </C>
    </Y>
  </X>
  <X>
    <Y>
      <A>
        <Key1>1</Key1>
        <Key2>2</Key2>
        <Key3>3</Key3>
      </A>
    </Y>
  </X>
  <X>
    <Y>
      <B>
         <Key1>4</Key1>
         <Key2>5</Key2>
      </B>
    </Y>
  </X>
  <X>
    <Z>
      <A>
        <Key1>7</Key1>
        <Key2>8</Key2>
        <Key3>9</Key3>
      </A>
    </Z>
  </X>
  <X>
    <Z>
      <A>
        <Key1>15</Key1>
        <Key2>18</Key2>
      </A>
    </Z>
  </X>
</root>