2012-04-03 18 views
-2

xmlのヘルプに必要なカスタム文字列形式。 JavaとC#。カスタム文字列をxmlに変換

[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とコードプロジェクトで提供されているいくつかの例を使用してシリアライズを試みましたが、私は 以下の形式で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] 
+1

XMLのようには見えません。あなたのコードはどこですか?あなたは何に問題がありますか?そしてJavaやC# - ひとつを選んで、無関係です。 – Mat

+0

@Mat - 私の質問が混乱していたら申し訳ありません。私はn個のカスタムフォーマットされた文字列を持っています。 [Key(Value)] [Key(Value)] ... [Key(Value)]の組の可変数。これをXMLファイルにシリアル化したいと思います。私の最初の記事で述べたように、xml形式が間違っている場合は、それを無視し、これを達成するための最善の方法をアドバイスしてください。ありがとう。 –

+0

おっと、忘れました。これはJava上です。ありがとう。 –

答えて

1

はあなたの目的のために、パーサクラスです:ここから

<?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> 

移動し、それを最適化:

String input; // [Node(X)][CHILD0(Y)][OBJECT1(A)][Key1(1)][Key2(2)]... 
String xml = new XmlSerializer(input).toXML(); 
System.out.println(xml); 

が出力:

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; 
} 

} 

はこのようにそれを使用します少し、例えばあなたがリピートしたくない場合d <X>...</X>ノード。

+0

ありがとう非常にjabu。あなたの時間と労力を感謝します。 –