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

查看:58
本文介绍了如何在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 元素组成,每个元素包含名为 firstname lastname nickname 和 salary .

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,在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天全站免登陆