簡體   English   中英

如何解析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. 整個行包括content,p,span標簽

如果指定了elementID我希望從中獲取內容?

我對XML不太熟悉,所以不確定是否可以實現?

使用以下命令將xml加載到對象中: simplexml_load_string()

然后在該對象上使用->xpath('RichText')以獲取RichText元素。

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

您會收到"<content><p><span>Address Line 1</span></p></content>"

它總是"<content><p><span>"嗎?

那么您可以使用(string) $RichText->content[0]->p[0]->span[0]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM