java - 使用java解析xml(w3.org)

标签 java xml

我需要解析 XML,下面给出了 XML 的某些部分:

<?xml version="1.0" encoding="utf-8"?>
<Document>
    <Sitemap>
        <TreeMap>
            <RootNodes>
                <TreeMapNode>
                    <NodeType>PackageHandle</NodeType>
                    <NodeValue>Page</NodeValue>
                    <ChildNodes />
                </TreeMapNode>
            </RootNodes>
        </TreeMap>
    </Sitemap>

    <Mastermap>
        <TreeMap>
            <RootNodes>
                <TreeMapNode>
                    <NodeType>Folder</NodeType>
                    <NodeValue>Template</NodeValue>
                    <ChildNodes>
                        <TreeMapNode>
                            <NodeType>PackageHandle</NodeType>
                            <NodeValue>Master Page</NodeValue>
                            <ChildNodes />
                        </TreeMapNode>
                    </ChildNodes>
                </TreeMapNode>
            </RootNodes>
        </TreeMap>
    </Mastermap>

    <Pages>
        <Page>
            <Diagram>
                <Widgets>
                    <Image>
                        <Name/>
                        <Rectangle>
                            <Rectangle X="0" Y="4" Width="130" Height="28" />
                        </Rectangle>
                        <Bold>False</Bold>
                        <BorderColor>Color(argb) = (255, 0, 0, 0)</BorderColor>
                        <BorderWidth>-1</BorderWidth>
                        <FillColor>Color(argb) = (255, 255, 255, 255)</FillColor>
                        <FontName>Arial</FontName>
                        <FontSize>9.75</FontSize>
                        <ForeColor>Color(argb) = (255, 0, 0, 0)</ForeColor>
                        <HorizontalAlignment>Center</HorizontalAlignment>
                        <Italic>False</Italic>
                        <Underline>False</Underline>
                        <VerticalAlignment>Center</VerticalAlignment>
                        <Widgets>
                            <TextPanel>
                                <Html>&lt;p style="font-size:13px;text-align:center;line-height:normal;"&gt;&lt;span style="font-family:'Arial Regular', 'Arial';font-weight:400;font-style:normal;font-size:13px;color:#000000;text-align:center;line-height:normal;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;</Html>
                                <Name />
                                <Rectangle>
                                    <Rectangle X="2" Y="6" Width="126" Height="16" />
                                </Rectangle>
                                <Bold>False</Bold>
                                <BorderColor>Color(argb) = (255, 0, 0, 0)</BorderColor>
                                <BorderWidth>-1</BorderWidth>
                                <FillColor>Color(argb) = (255, 255, 255, 255)</FillColor>
                                <FontName>Arial</FontName>
                                <FontSize>9.75</FontSize>
                                <ForeColor>Color(argb) = (255, 0, 0, 0)</ForeColor>
                                <HorizontalAlignment>Center</HorizontalAlignment>
                                <Italic>False</Italic>
                                <Underline>False</Underline>
                                <VerticalAlignment>Center</VerticalAlignment>
                            </TextPanel>
                        </Widgets>
                    </Image>
                    <Shape>

我需要阅读此内容并以所需的 XML 格式编写此内容。 我的代码如下:

public static void main(String[] args) throws SAXException, IOException,ParserConfigurationException, TransformerException 
    {

        DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
        Document document = docBuilder.parse(new File("C:/Users/ve00p5199/Desktop/Axure.xml"));
        NodeList nodeList = document.getElementsByTagName("*");
        System.out.println("total nodes="+nodeList.getLength());

        for (int i = 0; i < nodeList.getLength(); i++) {
            Node node = nodeList.item(i);
            if(node.getNodeType() != Node.ELEMENT_NODE){
                System.out.print(node.getNodeName()+"= ");
                 System.out.println(node.getTextContent());
            }
            else if (node.getNodeType() == Node.ELEMENT_NODE) {
                // do something with the current element
                 System.out.print(node.getNodeName()+"= ");
                 System.out.println(((Node) node.getChildNodes()).getNodeValue());  //giving NULL
System.out.println(node.getNodeValue());//giving NULL
            }
        }
    }
}

我想打印标签及其值。请建议保存/打印标签名称及其值所需的方法。

最佳答案

您需要使用以下两个函数来获取标记名及其文本内容 -

tag = ((Element)Node).getTagName() //or you can also use Node.getNodeName()
textValue = Node.getTextContent()

如果您不需要后代的文本内容,则必须获取每个节点的子节点并过滤掉类型为 Node.TEXT_NODE 的节点,然后打印 textContent 仅适用于 TEXT_NODE

示例 -

else if (node.getNodeType() == Node.ELEMENT_NODE) {
    // do something with the current element
    System.out.print(node.getNodeName()+"= ");
    NodeList cNodes = node.getChildNodes();
    for(int j = 0;j< cNodes.getLength();j++) {
        Node cN = cNodes.item(j);
        if(cN.getNodeType() == Node.TEXT_NODE) {
             System.out.println(cN.getTextContent());
        }
    }
}

请注意,这也会提供大量仅包含换行符和内容的文本内容,您可以根据需要添加自己的额外代码来过滤掉它们。

关于java - 使用java解析xml(w3.org),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31107580/

相关文章:

java - 即使解析了 XML,Document.toString() 也是 "[#document: null]"

jquery - 如何使用 Ajax/jQuery 通过服务器进程的 XML 输出更新表?

xml - JUnit 测试 XML 文档是否按任意列排序

java - 当我用鼠标拖动绘制圆形时,形状在某些情况下会移动

java - jms 生产者性能与 Spring

java - Jackson JSON 生成 HTTP 状态 500,XML 有效

python - BeautifulSoup 使用可迭代而不是字符串?

java - 从 Linux 获取 Java 应用程序的正确路径文件

Java BufferedImage 填充

java - 枚举、接口(interface)和 (Java 8) lambdas : code compiles but fails at runtime; is this expected?