我有一个XML文档,格式为:
<DynamicReport>
<DynamicReportDefId>501</DynamicReportDefId>
<DynamicReportDefName>Test report</DynamicReportDefName>
<DynamicReportDefSource>Live</DynamicReportDefSource>
<IsPrivate>false</IsPrivate>
<showSummaryRowsOnly>false</showSummaryRowsOnly>
<Limit>false</Limit>
<LimitRows/>
<InvUserId/>
<Version>0</Version>
<Category/>
<Columns class="array">
<Column>
<TableName>vwdynfirms</TableName>
<ColumnName>firmlongname</ColumnName>
<FormattingInfo>
<Header>
<DisplayName>Firm Long Name</DisplayName>
<FontInfo>
<Font/>
<FontSize/>
<IsBold/>
<IsItalic/>
<IsUnderline/>
<TextColor/>
<BackgroundColor/>
</FontInfo>
<AlignmentInfo>
<HorizontalAlignment/>
<VerticalAlignment/>
</AlignmentInfo>
<BorderInfo>
<BorderLeft/>
<BorderRight/>
<BorderTop/>
<BorderBottom/>
</BorderInfo>
</Header>
<Detail>
<FontInfo>
<Font/>
<FontSize/>
<IsBold/>
<IsItalic/>
<IsUnderline/>
<TextColor/>
<BackgroundColor/>
</FontInfo>
<AlignmentInfo>
<HorizontalAlignment/>
<VerticalAlignment/>
</AlignmentInfo>
<BorderInfo>
<BorderLeft/>
<BorderRight/>
<BorderTop/>
<BorderBottom/>
</BorderInfo>
<DisplayPattern/>
</Detail>
</FormattingInfo>
</Column>
<Column>
<TableName>vwdynfirms</TableName>
<ColumnName>firmname</ColumnName>
<FormattingInfo>
<Header>
<DisplayName>Firm Name</DisplayName>
<FontInfo>
<Font>Calibri</Font>
<FontSize>14</FontSize>
<IsBold>true</IsBold>
<IsItalic/>
<IsUnderline/>
<TextColor>#FF0080</TextColor>
<BackgroundColor>#FFFFFF</BackgroundColor>
</FontInfo>
<AlignmentInfo>
<HorizontalAlignment>left</HorizontalAlignment>
<VerticalAlignment>bottom</VerticalAlignment>
</AlignmentInfo>
<BorderInfo>
<BorderLeft/>
<BorderRight/>
<BorderTop/>
<BorderBottom>true</BorderBottom>
</BorderInfo>
</Header>
<Detail>
<FontInfo>
<Font/>
<FontSize/>
<IsBold/>
<IsItalic/>
<IsUnderline/>
<TextColor/>
<BackgroundColor/>
</FontInfo>
<AlignmentInfo>
<HorizontalAlignment/>
<VerticalAlignment/>
</AlignmentInfo>
<BorderInfo>
<BorderLeft/>
<BorderRight/>
<BorderTop/>
<BorderBottom/>
</BorderInfo>
<DisplayPattern/>
</Detail>
</FormattingInfo>
</Column>
</Columns>
</DynamicReport>
从这个xml文档中,我想获取特定TextColor
的{{1}}和BackgroundColor
,然后将它们存储在地图中。我希望地图的格式为Column
其中的键是Map<String>, List<String>
,字符串列表将是两种颜色(ColumnName
和TextColor
),现在是地图中列的值。
我已经编写了代码,其中直到可以通过遍历获得列名之前,我什么都不返回:
BackgroundColor
我能够获取文本颜色和背景颜色。但是我不知道如何遍历并获取相应的列名并将其添加到地图。任何帮助,将不胜感激。预先感谢。
答案 0 :(得分:1)
虽然这不是您要的,但我认为最好使用XPath来定位每个<Column>
元素,然后重用XPath
实例来定位相对于它的数据:
public static Map<String, List<String>> getColors(Document doc) {
XPath xpath = XPathFactory.newInstance().newXPath();
try {
NodeList columns = (NodeList)
xpath.evaluate("/DynamicReport/Columns/Column", doc,
XPathConstants.NODESET);
int numColumns = columns.getLength();
Map<String, List<String>> map = new LinkedHashMap<>(numColumns);
for (int i = 0; i < numColumns; i++) {
Node column = columns.item(i);
String columnName = xpath.evaluate("ColumnName", column);
Node fontInfo = (Node) xpath.evaluate(
"FormattingInfo/Header/FontInfo", column,
XPathConstants.NODE);
String textColor =
xpath.evaluate("TextColor", fontInfo);
String backgroundColor =
xpath.evaluate("BackgroundColor", fontInfo);
map.put(columnName, Arrays.asList(textColor, backgroundColor));
}
return map;
} catch (XPathException e) {
throw new RuntimeException(e);
}
}
如果您没有将XPathConstants常量传递给XPath.evaluate
,则将返回匹配节点的文本内容,这恰恰是获取字符串所要的内容。
答案 1 :(得分:0)
如果您不介意的话:)
Element column = (Element)(eElement.getParentNode().getParentNode().getParentNode());
答案 2 :(得分:0)
我能够获取文本颜色和背景颜色。但是我没有 关于如何回溯并获得相应列的线索 命名并将其添加到地图。
只需评估此XPath表达式(使用您在其中找到的当前上下文节点:/DynamicReport/Columns/Column/FormattingInfo/Header/FontInfo
):
ancestor::Column[1]/ColumnName