解析SGML并将其存储在PHP数组中
如果你能帮上忙,你就是个天才解析SGML并将其存储在PHP数组中,php,regex,string,text-extraction,information-extraction,Php,Regex,String,Text Extraction,Information Extraction,如果你能帮上忙,你就是个天才 - parent wealthy/ parent poor /parent something else -- parent -- children male/ children female / children something else -- child -- (name of the child is given without any tags around it) -- hobbies
- parent wealthy/ parent poor /parent something else
-- parent
-- children male/ children female / children something else
-- child
-- (name of the child is given without any tags around it)
-- hobbies
基本上,我会有一些这样的文本:
<parent wealthy>
<parent>
<children female>
<child>
jessica
<hobbies>
basketball, soccer, video games
</hobbies>
</child>
<child>
jane
<hobbies>
cooking, shopping, boys
</hobbies>
</child>
</children female>
<children male>
<child>
josh
<hobbies>
tennis, swimming
</hobbies>
</child>
</children male>
</parent>
</parent wealthy>
<parent poor>
<parent>
<children male>
<child>
---
<hobbies>...</hobbies>
</child>
</children male>
</parent>
</parent poor>
- parent wealthy/ parent poor /parent something else
-- parent
-- children male/ children female / children something else
-- child
-- (name of the child is given without any tags around it)
-- hobbies
<child>
jane
<hobbies>
cooking, shopping, boys
</hobbies>
</child>
我想知道我怎么可能解析出所有这些信息,并将它们存储在php数组/对象/变量中,同时保持它们出现的顺序?例如,如果
出现在
上方,我希望它们保持相同的顺序,如果
出现在
之前,情况也一样
- parent wealthy/ parent poor /parent something else
-- parent
-- children male/ children female / children something else
-- child
-- (name of the child is given without any tags around it)
-- hobbies
这将是几乎完全有效的XML,我可以使用SimpleXML来解析它,但是问题是,孩子的名字不会出现在任何标记之间,客户端希望以这种方式保留它,以便于用户使用。例如:
- parent wealthy/ parent poor /parent something else
-- parent
-- children male/ children female / children something else
-- child
-- (name of the child is given without any tags around it)
-- hobbies
<child>
jane
<hobbies>
cooking, shopping, boys
</hobbies>
</child>
text text test <hobbies>...<hobbies>. text text text <age>30</age>
简
做饭,购物,孩子们
这里“jane”出现在任何标记之外,而
出现在一些标记之间
- parent wealthy/ parent poor /parent something else
-- parent
-- children male/ children female / children something else
-- child
-- (name of the child is given without any tags around it)
-- hobbies
这是如何解析的?请给我一些建议。如果您建议使用regexp,请提供可用于您的答案被接受的regexp,因为我不知道regexp
- parent wealthy/ parent poor /parent something else
-- parent
-- children male/ children female / children something else
-- child
-- (name of the child is given without any tags around it)
-- hobbies
谢谢
- parent wealthy/ parent poor /parent something else
-- parent
-- children male/ children female / children something else
-- child
-- (name of the child is given without any tags around it)
-- hobbies
编辑:主要问题是客户端希望将普通文本与标记中的文本混合。例如:
- parent wealthy/ parent poor /parent something else
-- parent
-- children male/ children female / children something else
-- child
-- (name of the child is given without any tags around it)
-- hobbies
<child>
jane
<hobbies>
cooking, shopping, boys
</hobbies>
</child>
text text test <hobbies>...<hobbies>. text text text <age>30</age>
文本测试。。。。正文30
如何解析它?当使用这样的标记时:
<parent wealthy>
<parent>
<children female>
<child>
jessica
<hobbies>
basketball, soccer, video games
</hobbies>
</child>
<child>
jane
<hobbies>
cooking, shopping, boys
</hobbies>
</child>
</children female>
<children male>
<child>
josh
<hobbies>
tennis, swimming
</hobbies>
</child>
</children male>
</parent>
</parent wealthy>
<parent poor>
<parent>
<children male>
<child>
---
<hobbies>...</hobbies>
</child>
</children male>
</parent>
</parent poor>
- parent wealthy/ parent poor /parent something else
-- parent
-- children male/ children female / children something else
-- child
-- (name of the child is given without any tags around it)
-- hobbies
<child>
jane
<hobbies>
cooking, shopping, boys
</hobbies>
</child>
简
做饭,购物,孩子们
当使用SimpleXML解析时,jane
将位于child
元素的nodeValue
属性中
- parent wealthy/ parent poor /parent something else
-- parent
-- children male/ children female / children something else
-- child
-- (name of the child is given without any tags around it)
-- hobbies
只需记住
trim()
值,因为它可能包含空白,因为以下标记。当使用这样的标记时:
<parent wealthy>
<parent>
<children female>
<child>
jessica
<hobbies>
basketball, soccer, video games
</hobbies>
</child>
<child>
jane
<hobbies>
cooking, shopping, boys
</hobbies>
</child>
</children female>
<children male>
<child>
josh
<hobbies>
tennis, swimming
</hobbies>
</child>
</children male>
</parent>
</parent wealthy>
<parent poor>
<parent>
<children male>
<child>
---
<hobbies>...</hobbies>
</child>
</children male>
</parent>
</parent poor>
- parent wealthy/ parent poor /parent something else
-- parent
-- children male/ children female / children something else
-- child
-- (name of the child is given without any tags around it)
-- hobbies
<child>
jane
<hobbies>
cooking, shopping, boys
</hobbies>
</child>
简
做饭,购物,孩子们
当使用SimpleXML解析时,jane
将位于child
元素的nodeValue
属性中
- parent wealthy/ parent poor /parent something else
-- parent
-- children male/ children female / children something else
-- child
-- (name of the child is given without any tags around it)
-- hobbies
请记住
trim()
值,因为它可能包含空格,因为以下标记。我看到您在其中一个答案上的答复是。。。客户希望它对用户友好,以便人们键入此内容。
- parent wealthy/ parent poor /parent something else
-- parent
-- children male/ children female / children something else
-- child
-- (name of the child is given without any tags around it)
-- hobbies
XML结构是输入信息的最不友好的方式之一。事实上,这是非常受虐的,而是使用yaml并用解析它。我看到你在其中一个答案上的回答是。。。客户希望它对用户友好,以便人们键入此内容。
- parent wealthy/ parent poor /parent something else
-- parent
-- children male/ children female / children something else
-- child
-- (name of the child is given without any tags around it)
-- hobbies
XML结构是输入信息的最不友好的方式之一。实际上,这是一个非常自虐的问题,而是使用yaml并用解析它。我觉得人们试图从技术角度回答这个问题,但这里的问题是过程
- parent wealthy/ parent poor /parent something else
-- parent
-- children male/ children female / children something else
-- child
-- (name of the child is given without any tags around it)
-- hobbies
为什么,为什么?你的客户坚持要输入这样的数据?这是完全荒谬的。你会有一个噩梦,甚至验证它。更不用说正确地解析它了
- parent wealthy/ parent poor /parent something else
-- parent
-- children male/ children female / children something else
-- child
-- (name of the child is given without any tags around it)
-- hobbies
告诉他/她你为他们推出了一个像样的用户界面,选择自己的存储机制,这样就可以通过这样输入来缓解用户可能遇到的所有问题/问题和不正确的格式。这太疯狂了
- parent wealthy/ parent poor /parent something else
-- parent
-- children male/ children female / children something else
-- child
-- (name of the child is given without any tags around it)
-- hobbies
另一个完全不同的注意事项是,孩子似乎来自父母中的一方。我不知道智人是自动通婚的。我觉得人们试图从技术角度回答这个问题,但这里的问题是过程
- parent wealthy/ parent poor /parent something else
-- parent
-- children male/ children female / children something else
-- child
-- (name of the child is given without any tags around it)
-- hobbies
为什么,为什么?你的客户坚持要输入这样的数据?这是完全荒谬的。你会有一个噩梦,甚至验证它。更不用说正确地解析它了
- parent wealthy/ parent poor /parent something else
-- parent
-- children male/ children female / children something else
-- child
-- (name of the child is given without any tags around it)
-- hobbies
告诉他/她你为他们推出了一个像样的用户界面,选择自己的存储机制,这样就可以通过这样输入来缓解用户可能遇到的所有问题/问题和不正确的格式。这太疯狂了
- parent wealthy/ parent poor /parent something else
-- parent
-- children male/ children female / children something else
-- child
-- (name of the child is given without any tags around it)
-- hobbies
另一个完全不同的注意事项是,孩子似乎来自父母中的一方。我不知道智人是自动通婚的。这是一个XML文件,你应该远离正则表达式来阅读它。请尝试创建一个更具描述性的问题标题。实际上,它不是XML。所需的顺序和没有值的属性构成了这个SGML。单击upvote:请,下次你为你的问题提供真正的描述性标题,而不是你使用的hulaboo。多么糟糕的层次结构。好吧,这是一个XML文件,您应该远离正则表达式来阅读它。请尝试创建一个更具描述性的问题标题。实际上,它不是XML。所需的顺序和没有值的属性构成了这个SGML。单击upvote:请,下次为您的问题创建真正的描述性标题,而不是您使用的hulaboo。多么糟糕的层次结构。是的,但我不能这样做,这就是问题所在。客户希望它能方便用户输入。那么我不明白你的问题是什么。你能举例说明你需要它是什么样子吗?也许你误解了?我重新表述了我的答案。啊,好吧,我现在明白了,但是如果最后一个
”标记下面有更多的文本怎么办?e、 g
jane..一些额外的文本`?很棘手。在这种情况下,您必须找到一种方法来访问节点的“全文”值(包括XML元素子元素),并在第一次之前手动解析出所有内容。是的,但我不能这样做,这就是问题所在。客户希望它能方便用户输入。那么我不明白你的问题是什么。你能举例说明你需要它是什么样子吗?也许你误解了?我重新表述了我的答案。啊,好吧,我现在明白了,但是如果最后一个”标记下面有更多的文本怎么办?e、 g
jane..一些额外的文本`?很棘手。在这种情况下,您必须找到一种方法来访问节点的“全文”值(包括XML元素子元素),并在第一个之前手动解析出所有内容,但YAML的问题仍然是一样的。客户端希望将普通文本与标记混合。例如:jenny..更多的文本。。。这就是问题所在。“YAML能帮上忙吗?如果能,怎么做?”点击Upvot
- parent wealthy/ parent poor /parent something else
-- parent
-- children male/ children female / children something else
-- child
-- (name of the child is given without any tags around it)
-- hobbies