如何从XML Document获取另一个节点内的节点的值?

时间:2016-03-01 17:20:16

标签: xpath liferay liferay-6 dom4j

Liferay使用 DOM4J Parser来解析XML文档,它有一个名为 WCM Structures 的编辑器,允许您创建一系列字段/节点: / p>

enter image description here

编辑器生成此XML文档:

Sub Keysha_Bee()
    Dim wb1 As Workbook, wb As Workbook
    Dim ws1 As Worksheet
    Dim wb2 As Workbook
    Dim ws2 As Worksheet
    Dim SheetID As String
    Dim i As Integer
    Dim lrow As Integer

    Set wb2 = ThisWorkbook '<<edited
    'get the "other open workbook" (must have only the 2 open!)
    For Each wb In Application.WorkBooks
        If wb.Name <> wb2.Name then
            Set wb1 = wb
            Exit For
        End If
    Next wb
    If wb1 Is Nothing Then
        MsgBox "No other workbook open!"
        Exit Sub
    End If

    Set ws1 = wb1.Sheets(1)

    If InStr(ws1.Range("B3"), "FPPI") > 0 Then SheetID = "FPPI-Routed"
    If InStr(ws1.Range("B3"), "USPPI") > 0 Then SheetID = "USPPI-Routed"
    If InStr(ws1.Range("B3"), "Standard") > 0 Then SheetID = "Standard"

    On Error Resume Next 'ignore any error
    Set ws2 = wb2.Worksheets(SheetID)
    On Error GoTo 0      'stop ignoring errors

    'was ws2 set ?
    If ws2 Is Nothing Then
        MsgBox "Sheet '" & SheetID & "' was not found!", vbExclamation
        Exit Sub
    End If

    lrow = ws2.Cells(Rows.Count, 2).End(xlUp).Row + 1
    ws2.Cells(lrow, 2) = ws1.Range("D6") 'Customer Name
    If ws2.Range("D14") = "" Then
        ws2.Cells(lrow, 3) = ws1.Range("D17") 'Agent's Name
        ws2.Cells(lrow, 4) = ws1.Range("D18") 'Auth Agent's Email
    Else
        ws2.Cells(lrow, 3) = ws1.Range("D15") 'Agent's Name
        ws2.Cells(lrow, 4) = ws1.Range("D16") 'Auth Agent's Email
    End If
    ws2.Cells(lrow, 5) = "NO" 'Routed, not sure what this is supposed to reference
    ws2.Cells(lrow, 6) = ws1.Range("D20") ' Routed
    ws2.Cells(lrow, 7) = ws1.Range("D26") ' Origin
    ws2.Cells(lrow, 8) = ws1.Range("D27") ' Hazardous
    ws2.Cells(lrow, 9) = ws1.Range("D28") ' UC Type
    ws2.Cells(lrow, 10) = "Date" 'Not sure what this is supposed to refference

End Sub

然后通过使用FreeMarker(基于 Java 的模板引擎),我可以通过调用<root available-locales="en_US" default-locale="en_US"> <dynamic-element fieldNamespace="ddm" indexType="keyword" localizable="true" name="Separator7562" readOnly="false" repeatable="false" type="ddm-separator"> <!-- meta data --> <dynamic-element dataType="string" indexType="keyword" localizable="true" multiple="false" name="Select" readOnly="false" repeatable="false" type="select"> <dynamic-element name="option11132" type="option" value="value 1"> <meta-data locale="en_US"> <entry name="label"> <![CDATA[Label]]> </entry> </meta-data> </dynamic-element> <dynamic-element name="option11133" type="option" value="value 2"> <!-- meta data --> </dynamic-element> <dynamic-element name="option11134" type="option" value="value 3"> <!-- meta data --> </dynamic-element> </dynamic-element> </dynamic-element> </root> document.selectNodes()来使用XPath Expression获取所有节点。 我的问题是我无法在另一个节点内获取节点的值(参见XML)。

document.selectSingleNode()

我收到此错误:

<#if className == "com.liferay.portlet.journal.model.JournalArticle">
    <#assign journalArticle = assetRenderer.getArticle() />
    <#assign document = saxReaderUtil.read(journalArticle.getContentByLocale(locale.toString())) />

    <#-- retrieve fields from structure -->
    <#assign separatorNodes = document.selectNodes("/root/dynamic-element[@name='Separator7562']/dynamic-content") />

    <#list separatorNodes as node>
        <#assign selectValue = node.selectSingleNode("dynamic-element[@name='Select']").getText() />
    </#list>

    Option: ${selectValue}
</#if>

0 个答案:

没有答案