如何在 Dom 解析器中修改 XML 数据 [英] How to modify XML data in Dom parser

查看:24
本文介绍了如何在 Dom 解析器中修改 XML 数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Java 和 XML DOM 解析器的新手.我有一个要求,比如读取 xml 数据并将其存储为列和行类型的通知.示例:sample.xml 文件

I am new working in Java and XML DOM parser. I had a requirement like read the xml data and store it inform of column and rows type. Example:sample.xml file

<staff>
        <firstname>Swetha</firstname>
        <lastname>EUnis</lastname>
        <nickname>Swetha</nickname>
        <salary>10000</salary>
    </staff>
    <staff>
        <firstname>John</firstname>
        <lastname>MAdiv</lastname>
        <nickname>Jo</nickname>
        <salary>200000</salary>
    </staff>

我需要读取这个 XML 文件并以上述格式存储:

i need to read this XML file and store it in the above format:

firstName,lastName,nickName,Salary
swetha,Eunis,swetha,10000
john,MAdiv,Jo,200000

Java 代码:

NodeList nl= doc.getElementsByTagName("*");

        for(int i=0;i< nl.getLength();i++)
        {
            Element section = (Element) nl.item(i);

             Node title = section.getFirstChild();
              while (title != null && title.getNodeType() != Node.ELEMENT_NODE)
              {
                   title = title.getNextSibling();
                if (title != null)
              {
                String first=title.getFirstChild().getNodeValue().trim();
                    if(first!=null)
                    {
                        title = title.getNextSibling();
                    }
                System.out.print(first + ",");
               } }
              System.out.println("");
        }//for

我做了上面的代码,但我无法找到以上述列和行格式获取数据的方法.任何人都可以请帮我解决我的问题,我从过去的很多天里都在调查它

I did the above code, but i am not able to find the way to get the data in the above column and row format. Can any one please please kindly help me in solving my issue, i am looking into it from past many days

推荐答案

由于这看起来像家庭作业,所以我会给你一些提示:

Since this looks like homework, I'm going to give you some hints:

  • 很可能你的讲师给了你一些关于处理 XML DOM 的讲义和/或示例.再读一遍.

  • The chances are that your lecturer has given you some lecture notes and/or examples on processing an XML DOM. Read them all again.

getElementsByTagName 方法将元素名称作为参数."*" 不是有效的元素名称,因此调用不会返回任何内容.

The getElementsByTagName method takes an element name as a parameter. "*" is not a valid element name, so the call won't return anything.

您的代码需要反映 XML 的结构.本例中的 XML 结构由 N 个 staff 元素组成,每个元素都包含名为 firstnamelastnamenicknamesalary.

Your code needs to mirror the structure of the XML. The XML structure in this case consists of N staff elements, each of which contains elements named firstname, lastname, nickname and salary.

您的讲师也可能希望您使用类似 XSLT 或 XML 绑定机制的东西来简化这一点.(或者也许这个 旨在成为 XMI 而不是 XML ......其中还有其他方法来处理这个......)

It is also possible that your lecturer expects you to use something like XSLT or an XML binding mechanism to simplify this. (Or maybe this was intended to be XMI rather than XML ... in which there are other ways to handle this ...)

我保留getElementsByTagName方法参数*",因为要动态读取数据.

I kept getElementsByTagName method parameter "*" because to read the data dynamically.

好吧,它不起作用!!DOM getElementsByTagName 方法不接受任何类型的模式.

Well, it doesn't work!! The DOM getElementsByTagName method does NOT accept a pattern of any kind.

如果你想让你的代码通用,你不能使用getElementsByTagName.您需要从顶部开始遍历树,从 DOM 的根节点开始.

If you want to make your code generic, you can't use getElementsByTagName. You will need to walk the tree from the top, starting with the DOM's root node.

能否请您提供示例数据.

Can you please provide me with sample data.

没有.你的讲师不同意我给你复制代码.但是,我要指出,网络上有很多 XML DOM 教程,它们应该可以帮助您弄清楚您需要做什么.最好的事情是你自己做这项工作.这样你会学到更多……这就是你作业的重点!

No. Your lecturer would not approve of me giving you code to copy from. However, I will point out that there are lots of XML DOM tutorials on the web which should help you figure out what you need to do. The best thing is for you to do the work yourself. You will learn more that way ... and that is the whole point of your homework!

这篇关于如何在 Dom 解析器中修改 XML 数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆