如何从此 XML 文档中获取祖先节点?

huangapple go评论65阅读模式
英文:

How to get the ancestor node from this XML document?

问题

//Getting the column from the colors for all the columns
public static Map<String, List<String>> getColors(Document doc) {
    String expressionGroupedCol = "/DynamicReport/Columns/Column/FormattingInfo/Header/FontInfo";
    XPath xPath = XPathFactory.newInstance().newXPath();
    Map<String, List<String>> colorMap = new HashMap<>();

    try {
        NodeList nodeList = (NodeList) xPath.compile(expressionGroupedCol).evaluate(
                doc, XPathConstants.NODESET);
        
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node nNode = nodeList.item(i);
            if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                Element eElement = (Element) nNode;
                String strTextColor = eElement.getElementsByTagName("TextColor").item(0).getTextContent();
                String strBackgroundColor = eElement.getElementsByTagName("BackgroundColor").item(0).getTextContent();
                
                // Traverse back to get the ColumnName
                Node columnNode = nNode.getParentNode().getParentNode();
                if (columnNode.getNodeType() == Node.ELEMENT_NODE) {
                    Element columnElement = (Element) columnNode;
                    String columnName = columnElement.getElementsByTagName("ColumnName").item(0).getTextContent();
                    
                    // Add to the map
                    colorMap.putIfAbsent(columnName, new ArrayList<>());
                    colorMap.get(columnName).add(strTextColor);
                    colorMap.get(columnName).add(strBackgroundColor);
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    
    return colorMap;
}

Note: I've provided the code based on your request for code translation. If you have any questions or need further assistance, feel free to ask.

英文:

I have an xml document, say of the format :

&lt;DynamicReport&gt;
&lt;DynamicReportDefId&gt;501&lt;/DynamicReportDefId&gt;
&lt;DynamicReportDefName&gt;Test report&lt;/DynamicReportDefName&gt;
&lt;DynamicReportDefSource&gt;Live&lt;/DynamicReportDefSource&gt;
&lt;IsPrivate&gt;false&lt;/IsPrivate&gt;
&lt;showSummaryRowsOnly&gt;false&lt;/showSummaryRowsOnly&gt;
&lt;Limit&gt;false&lt;/Limit&gt;
&lt;LimitRows/&gt;
&lt;InvUserId/&gt;
&lt;Version&gt;0&lt;/Version&gt;
&lt;Category/&gt;
&lt;Columns class=&quot;array&quot;&gt;
&lt;Column&gt;
&lt;TableName&gt;vwdynfirms&lt;/TableName&gt;
&lt;ColumnName&gt;firmlongname&lt;/ColumnName&gt;
&lt;FormattingInfo&gt;
&lt;Header&gt;
&lt;DisplayName&gt;Firm Long Name&lt;/DisplayName&gt;
&lt;FontInfo&gt;
&lt;Font/&gt;
&lt;FontSize/&gt;
&lt;IsBold/&gt;
&lt;IsItalic/&gt;
&lt;IsUnderline/&gt;
&lt;TextColor/&gt;
&lt;BackgroundColor/&gt;
&lt;/FontInfo&gt;
&lt;AlignmentInfo&gt;
&lt;HorizontalAlignment/&gt;
&lt;VerticalAlignment/&gt;
&lt;/AlignmentInfo&gt;
&lt;BorderInfo&gt;
&lt;BorderLeft/&gt;
&lt;BorderRight/&gt;
&lt;BorderTop/&gt;
&lt;BorderBottom/&gt;
&lt;/BorderInfo&gt;
&lt;/Header&gt;
&lt;Detail&gt;
&lt;FontInfo&gt;
&lt;Font/&gt;
&lt;FontSize/&gt;
&lt;IsBold/&gt;
&lt;IsItalic/&gt;
&lt;IsUnderline/&gt;
&lt;TextColor/&gt;
&lt;BackgroundColor/&gt;
&lt;/FontInfo&gt;
&lt;AlignmentInfo&gt;
&lt;HorizontalAlignment/&gt;
&lt;VerticalAlignment/&gt;
&lt;/AlignmentInfo&gt;
&lt;BorderInfo&gt;
&lt;BorderLeft/&gt;
&lt;BorderRight/&gt;
&lt;BorderTop/&gt;
&lt;BorderBottom/&gt;
&lt;/BorderInfo&gt;
&lt;DisplayPattern/&gt;
&lt;/Detail&gt;
&lt;/FormattingInfo&gt;
&lt;/Column&gt;
&lt;Column&gt;
&lt;TableName&gt;vwdynfirms&lt;/TableName&gt;
&lt;ColumnName&gt;firmname&lt;/ColumnName&gt;
&lt;FormattingInfo&gt;
&lt;Header&gt;
&lt;DisplayName&gt;Firm Name&lt;/DisplayName&gt;
&lt;FontInfo&gt;
&lt;Font&gt;Calibri&lt;/Font&gt;
&lt;FontSize&gt;14&lt;/FontSize&gt;
&lt;IsBold&gt;true&lt;/IsBold&gt;
&lt;IsItalic/&gt;
&lt;IsUnderline/&gt;
&lt;TextColor&gt;#FF0080&lt;/TextColor&gt;
&lt;BackgroundColor&gt;#FFFFFF&lt;/BackgroundColor&gt;
&lt;/FontInfo&gt;
&lt;AlignmentInfo&gt;
&lt;HorizontalAlignment&gt;left&lt;/HorizontalAlignment&gt;
&lt;VerticalAlignment&gt;bottom&lt;/VerticalAlignment&gt;
&lt;/AlignmentInfo&gt;
&lt;BorderInfo&gt;
&lt;BorderLeft/&gt;
&lt;BorderRight/&gt;
&lt;BorderTop/&gt;
&lt;BorderBottom&gt;true&lt;/BorderBottom&gt;
&lt;/BorderInfo&gt;
&lt;/Header&gt;
&lt;Detail&gt;
&lt;FontInfo&gt;
&lt;Font/&gt;
&lt;FontSize/&gt;
&lt;IsBold/&gt;
&lt;IsItalic/&gt;
&lt;IsUnderline/&gt;
&lt;TextColor/&gt;
&lt;BackgroundColor/&gt;
&lt;/FontInfo&gt;
&lt;AlignmentInfo&gt;
&lt;HorizontalAlignment/&gt;
&lt;VerticalAlignment/&gt;
&lt;/AlignmentInfo&gt;
&lt;BorderInfo&gt;
&lt;BorderLeft/&gt;
&lt;BorderRight/&gt;
&lt;BorderTop/&gt;
&lt;BorderBottom/&gt;
&lt;/BorderInfo&gt;
&lt;DisplayPattern/&gt;
&lt;/Detail&gt;
&lt;/FormattingInfo&gt;
&lt;/Column&gt;
&lt;/Columns&gt;
&lt;/DynamicReport&gt;

From this xml document, I want to get the TextColor and the BackgroundColor for a particular Column and then store them in a map. I am expecting the map to be of the format Map&lt;String&gt;, List&lt;String&gt;
where the key is the ColumnName and the list of strings would be the two colors (TextColor and the BackgroundColor) as the values for the column in the map for now.

I have written the code, wherein I am not returning anything until I am able to get the column name by traversing back :

    //Getting the column from the colors for all the columns
public static void getColors(Document doc)
{
String expressionGroupedCol = &quot;/DynamicReport/Columns/Column/FormattingInfo/Header/FontInfo&quot;;
XPath xPath = XPathFactory.newInstance().newXPath();
try {
NodeList nodeList = (NodeList) xPath.compile(expressionGroupedCol).evaluate(
doc, XPathConstants.NODESET);
for (int i = 0; i &lt; nodeList.getLength(); i++)
{
Node nNode = nodeList.item(i);
if (nNode.getNodeType() == Node.ELEMENT_NODE)
{
Element eElement = (Element) nNode;
String strTextColor = eElement.getElementsByTagName(&quot;TextColor&quot;).item(0).getTextContent();
System.out.println(strTextColor);
String strBackgroundColor = eElement.getElementsByTagName(&quot;BackgroundColor&quot;).item(0).getTextContent();
System.out.println(strBackgroundColor);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}

I am able to get the text color and background color. But I don't have a clue as to how to traverse back and get the corresponding column name and add that to the map. Any help would be appreciated. Thanks in advance.

答案1

得分: 1

虽然这不是你所要求的内容,但我认为你最好使用XPath来定位每个&lt;Column&gt;元素,然后重用你的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,则返回匹配节点的文本内容,这恰好是你获取字符串所需的内容。

英文:

While this isn’t what you asked for, I think you would be better off using an XPath to locate each &lt;Column&gt; element, then reusing your XPath instance to locate the data relative to it:

public static Map&lt;String, List&lt;String&gt;&gt; getColors(Document doc) {
XPath xpath = XPathFactory.newInstance().newXPath();
try {
NodeList columns = (NodeList)
xpath.evaluate(&quot;/DynamicReport/Columns/Column&quot;, doc,
XPathConstants.NODESET);
int numColumns = columns.getLength();
Map&lt;String, List&lt;String&gt;&gt; map = new LinkedHashMap&lt;&gt;(numColumns);
for (int i = 0; i &lt; numColumns; i++) {
Node column = columns.item(i);
String columnName = xpath.evaluate(&quot;ColumnName&quot;, column);
Node fontInfo = (Node) xpath.evaluate(
&quot;FormattingInfo/Header/FontInfo&quot;, column,
XPathConstants.NODE);
String textColor =
xpath.evaluate(&quot;TextColor&quot;, fontInfo);
String backgroundColor =
xpath.evaluate(&quot;BackgroundColor&quot;, fontInfo);
map.put(columnName, Arrays.asList(textColor, backgroundColor));
}
return map;
} catch (XPathException e) {
throw new RuntimeException(e);
}
}

If you don’t pass an XPathConstants constant to XPath.evaluate, the text content of the matching node is returned, which turns out to be exactly what you want for obtaining your Strings.

答案2

得分: 0

如果你不介意作弊的话 如何从此 XML 文档中获取祖先节点?

Element column = (Element)(eElement.getParentNode().getParentNode().getParentNode());
英文:

If you don't mind cheesing it 如何从此 XML 文档中获取祖先节点?

Element column = (Element)(eElement.getParentNode().getParentNode().getParentNode());

答案3

得分: 0

我能够获取文本颜色和背景颜色。但是我不知道如何遍历回去获取相应的列名,并将其添加到映射中。

只需评估此XPath表达式(使用您当前通过此路径找到的上下文节点:/DynamicReport/Columns/Column/FormattingInfo/Header/FontInfo):

ancestor::Column[1]/ColumnName
英文:

> I am able to get the text color and background color. But I don't have
> a clue as to how to traverse back and get the corresponding column
> name and add that to the map.

Just evaluate this XPath expression (using your current context node that you found with this: /DynamicReport/Columns/Column/FormattingInfo/Header/FontInfo):

ancestor::Column[1]/ColumnName

huangapple
  • 本文由 发表于 2020年10月19日 00:11:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/64415491.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定