【问题标题】:how to parse xml file and grab text values如何解析 xml 文件并获取文本值
【发布时间】:2023-04-03 04:35:01
【问题描述】:

以下只是一个大 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、span 标签在内的整行

如果我指定 elementID 我想要的内容来自?

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

【问题讨论】:

    标签: php javascript xml xml-parsing fxg


    【解决方案1】:

    将 xml 加载到对象中:simplexml_load_string()

    然后在该对象上使用-&gt;xpath('RichText') 来获取 RichText 元素。

    如果你在这些元素上使用-&gt;asXML()

    你会得到"&lt;content&gt;&lt;p&gt;&lt;span&gt;Address Line 1&lt;/span&gt;&lt;/p&gt;&lt;/content&gt;"

    总是"&lt;content&gt;&lt;p&gt;&lt;span&gt;"

    那么你可以使用(string) $RichText-&gt;content[0]-&gt;p[0]-&gt;span[0]

    【讨论】:

    • 我认为它总会有 content,p,span 标签。
    • 有没有办法准确指定我想要从中检索元素的 RichText 节点。每个文件中通常会有 5 或 6 个 RichText 节点。我需要根据 RichText 节点的 elementID 获取特定的。如果我以某种方式指定 id,这可能吗?
    • 阅读有关 xpath 的更多信息,认为它类似于 ('RichText[id=testid]')
    • 非常感谢。我会进一步研究。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-20
    • 2018-04-05
    • 1970-01-01
    • 2017-08-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多