如何分析 XML 文件和抓取文本值


how to parse xml file and grab text values

以下只是一个大型fxg文件中的代码片段,基本上只是一个xml文件:

<RichText x="14.1655" y="46.5674" columnGap="18" columnCount="1" textAlign="left" fontFamily="Bootstrap" color="#53836A" whiteSpaceCollapse="preserve" width="202.712" height="13.334" s7:caps="none" s7:colorName="" s7:colorValue="#B24FA41C" s7:colorspace="cmyk" s7:elementID="line1" s7:fill="true" s7:fillOverprint="false" s7:firstBaselineOffset="ascent" s7:joints="miter" s7:maxFontSize="12" s7:miterLimit="10" s7:referencePoint="inherit" s7:rowCount="1" s7:rowGap="18" s7:rowMajorOrder="true" s7:stroke="false" s7:strokeOverprint="false" s7:warpBend="0.5" s7:warpDirection="horizontal" s7:warpHorizontalDistortion="0" s7:warpStyle="none" s7:warpVerticalDistortion="0" s7:weight="1" ai:aa="2" ATE:C_charRotation="0" ATE:C_horizontalScale="1" ATE:C_kerning="metric" ATE:C_verticalScale="1" ATE:P_autoHyphenate="true" ATE:P_consecutiveHyphenLimit="0" ATE:P_hyphenateCapitalized="true" ATE:P_hyphenatedWordSize="6" ATE:P_hyphenationPreference="0.5" ATE:P_hyphenationZone="36" ATE:P_postHyphenSize="2" ATE:P_preHyphenSize="2" d:userLabel="id:line1">
   <content><p><span>Address Line 1</span></p></content>
</RichText>

XML 文件中有许多节点具有类似的结构。但是每个RichText节点都有一个唯一的元素 ID,在本例中s7:elementID="line1"

使用 PHP 或 JavaScript,我该如何获取:

  1. 文本"地址行 1"
  2. 整行包括内容,P,跨度标签

如果我指定要从哪个elementID获取内容?

我对XML不是很熟悉,所以我不确定这是否可能?

xml 加载到一个对象中: simplexml_load_string()

,然后对该对象使用 ->xpath('RichText') 来获取富文本元素。

如果在 THOS 元素上使用->asXML()

你得到"<content><p><span>Address Line 1</span></p></content>"

总是"<content><p><span>"吗?

然后你可以使用(string) $RichText->content[0]->p[0]->span[0]