Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/370.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 Scala:从列表列表创建结构(XML)_Java_Xml_List_Scala - Fatal编程技术网

Java Scala:从列表列表创建结构(XML)

Java Scala:从列表列表创建结构(XML),java,xml,list,scala,Java,Xml,List,Scala,我有一个来自java的结构,一个包含 元素,如: [[ "Node0", "Node00", "Leaf0"], [ "Node0", "Node00", "Leaf1"], [ "Node1", "Leaf2"], [ "Node0", "Leaf3"], [ "Node2", "Node20", "Node200", "Leaf4"]] 我想做的是在中创建一个XML结构(使用Scala) 最简单的方法是,以下面这样的方式结束。 我可以用很多方法来实现这一点,迭代、递归调用等等 有没

我有一个来自java的结构,一个包含 元素,如:

[[ "Node0", "Node00", "Leaf0"],
 [ "Node0", "Node00", "Leaf1"],
 [ "Node1", "Leaf2"],
 [ "Node0", "Leaf3"],
 [ "Node2", "Node20", "Node200", "Leaf4"]]
我想做的是在中创建一个XML结构(使用Scala) 最简单的方法是,以下面这样的方式结束。 我可以用很多方法来实现这一点,迭代、递归调用等等

有没有关于解决这个问题的简洁易读的方法的建议

<node> Node0 
      <node> Node00 
            <node> Leaf0 </node>
            <node> Leaf1 </node>
      </node>
      <node> Leaf3 </node>
</node>
<node> Node1
      <node> Leaf2 </node>
</node>
<node> Node2
      <node> Node20
            <node> Node200
                  <node> Leaf4 </node>
            </node>
      </node>
</node>
Node0
节点00
叶0
叶1
叶3
节点1
叶子2
节点2
节点20
节点200
叶子4
尝试如何从Scala中的集合输出XML


此外,我还建议更可读的XML输出表单将节点名称放在属性(或子元素)中,而不是将文本与其他子元素混合。例如

<node name="Node0">
  <node name="Node00">


节点0
节点00
...
尝试如何从Scala中的集合输出XML


此外,我还建议更可读的XML输出表单将节点名称放在属性(或子元素)中,而不是将文本与其他子元素混合。例如

<node name="Node0">
  <node name="Node00">


节点0
节点00
...

好的,我试了一下。正如其他人所建议的,我也在使用属性。对于将生成以列表内容命名的元素(而不是使用属性)的版本,还有两行注释

作业的难点部分由下面的类完成,该类获取字符串列表并转换给定给它的节点,以便它们包含由列表表示的节点层次结构

import xml._
import transform._

class AddPath(l: List[String]) extends RewriteRule {
  def listToNodes(l: List[String]): Seq[Node] = l match {
    case Nil => Seq.empty
    case first :: rest => 
      <node>{listToNodes(rest)}</node> % Attribute("name", Text(first), Null)
    //case first :: rest => 
      //<node>{listToNodes(rest)}</node> copy (label =  first)
  }

  def transformChild(child: Seq[Node]) = l match {
    case Nil => child
    case first :: rest =>
      child flatMap {
        case elem: Elem if elem.attribute("name") exists (_ contains Text(first)) =>
        //case elem: Elem if elem.label == first =>
          new AddPath(rest) transform elem
        case other => Seq(other)
      }
  }

  def appendToOrTransformChild(child: Seq[Node]) = {
    val newChild = transformChild(child)
    if (newChild == child)
      child ++ listToNodes(l)
    else
      newChild
  }

  override
  def transform(n: Node): Seq[Node] = n match {
    case elem: Elem => elem.copy(child = appendToOrTransformChild(elem.child))
    case other => other
  }
}
接下来,我们用这些规则创建一个规则转换器

val ruleTransformer = new RuleTransformer(listOfAddPaths: _*)
最后,我们创建XML并将其打印出来。请注意,我正在添加一个根节点。如果你不想要它,只要得到它的
child
。还要注意,
ruleTransformer
将返回一个带有单个节点的
Seq[Node]
——我们的结果

val results = ruleTransformer(<root/>)
val prettyPrinter = new PrettyPrinter(80, 4)
results foreach { xml =>
  println(prettyPrinter format xml)
}
val结果=规则转换器()
val prettyPrinter=新的prettyPrinter(80,4)
结果foreach{xml=>
println(预打印格式xml)
}
以及输出:

<root>
    <node name="Node0">
        <node name="Node00">
            <node name="Leaf0"></node>
            <node name="Leaf1"></node>
        </node>
        <node name="Leaf3"></node>
    </node>
    <node name="Node1">
        <node name="Leaf2"></node>
    </node>
    <node name="Node2">
        <node name="Node20">
            <node name="Node200">
                <node name="Leaf4"></node>
            </node>
        </node>
    </node>
</root>

备用版本的输出:

<root>
    <Node0>
        <Node00>
            <Leaf0></Leaf0>
            <Leaf1></Leaf1>
        </Node00>
        <Leaf3></Leaf3>
    </Node0>
    <Node1>
        <Leaf2></Leaf2>
    </Node1>
    <Node2>
        <Node20>
            <Node200>
                <Leaf4></Leaf4>
            </Node200>
        </Node20>
    </Node2>
</root>

好的,我试了一下。正如其他人所建议的,我也在使用属性。对于将生成以列表内容命名的元素(而不是使用属性)的版本,还有两行注释

作业的难点部分由下面的类完成,该类获取字符串列表并转换给定给它的节点,以便它们包含由列表表示的节点层次结构

import xml._
import transform._

class AddPath(l: List[String]) extends RewriteRule {
  def listToNodes(l: List[String]): Seq[Node] = l match {
    case Nil => Seq.empty
    case first :: rest => 
      <node>{listToNodes(rest)}</node> % Attribute("name", Text(first), Null)
    //case first :: rest => 
      //<node>{listToNodes(rest)}</node> copy (label =  first)
  }

  def transformChild(child: Seq[Node]) = l match {
    case Nil => child
    case first :: rest =>
      child flatMap {
        case elem: Elem if elem.attribute("name") exists (_ contains Text(first)) =>
        //case elem: Elem if elem.label == first =>
          new AddPath(rest) transform elem
        case other => Seq(other)
      }
  }

  def appendToOrTransformChild(child: Seq[Node]) = {
    val newChild = transformChild(child)
    if (newChild == child)
      child ++ listToNodes(l)
    else
      newChild
  }

  override
  def transform(n: Node): Seq[Node] = n match {
    case elem: Elem => elem.copy(child = appendToOrTransformChild(elem.child))
    case other => other
  }
}
接下来,我们用这些规则创建一个规则转换器

val ruleTransformer = new RuleTransformer(listOfAddPaths: _*)
最后,我们创建XML并将其打印出来。请注意,我正在添加一个根节点。如果你不想要它,只要得到它的
child
。还要注意,
ruleTransformer
将返回一个带有单个节点的
Seq[Node]
——我们的结果

val results = ruleTransformer(<root/>)
val prettyPrinter = new PrettyPrinter(80, 4)
results foreach { xml =>
  println(prettyPrinter format xml)
}
val结果=规则转换器()
val prettyPrinter=新的prettyPrinter(80,4)
结果foreach{xml=>
println(预打印格式xml)
}
以及输出:

<root>
    <node name="Node0">
        <node name="Node00">
            <node name="Leaf0"></node>
            <node name="Leaf1"></node>
        </node>
        <node name="Leaf3"></node>
    </node>
    <node name="Node1">
        <node name="Leaf2"></node>
    </node>
    <node name="Node2">
        <node name="Node20">
            <node name="Node200">
                <node name="Leaf4"></node>
            </node>
        </node>
    </node>
</root>

备用版本的输出:

<root>
    <Node0>
        <Node00>
            <Leaf0></Leaf0>
            <Leaf1></Leaf1>
        </Node00>
        <Leaf3></Leaf3>
    </Node0>
    <Node1>
        <Leaf2></Leaf2>
    </Node1>
    <Node2>
        <Node20>
            <Node200>
                <Leaf4></Leaf4>
            </Node200>
        </Node20>
    </Node2>
</root>


我建议更具可读性的XML输出表单将节点名称放在属性(或子元素)中,而不是将文本与其他子元素混合。例如,
Node0 Node00…
如果列表是以Scala语法提供的,则会更有帮助。我建议更可读的XML输出表单将节点名称放在属性(或子元素)中,而不是将文本与其他子元素混合。例如,
Node0 Node00…
如果列表是以Scala语法提供的,则会更有帮助。感谢您近乎史诗般的回答。我真的没想到会有人编写运行的代码,只有一个指向正确方向的指针才行。非常感谢你们的宝贵努力,我从中学到了很多。感谢你们近乎史诗般的回答。我真的没想到会有人编写运行的代码,只有一个指向正确方向的指针才行。非常感谢你们的宝贵努力,我从中学到了很多。