动态读取java中的xml元素和值 [英] dynamically read xml element and value in java

查看:141
本文介绍了动态读取java中的xml元素和值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试阅读XML(无论如何,我不知道它的结构,bcz我正在从服务器上阅读flie。),我在SO上搜索了很多。但没有找到任何帮助。



1>我想检索XML的节点名称,从根到文件结尾,孩子的孩子或孩子,直到没有孩子或兄弟姐妹在类似的问题中,我发现他们使用标记名来检索该标记的值,因此我想在其中传递该节点名称以获取值。 ..



是否可以动态执行。或者必须遵循与先前问题相同的结构,例如静态添加标记名以检索值。



在java中执行此操作后,我想创建可执行jar,并希望在欢乐中使用它。






编辑
i希望找到元素及其值,以便在第一个条件下我循环我的arraylist,我将其命名为 masterheader 其中包含所有标记名称,在第二个循环中我将从xml中找到节点名称,并且在那里面两个for循环我指定了一个条件,如果两个匹配,那么它得到节点名称,它以分隔的形式存储一个值,因为我想在 csv 中转换它。



这里,masterheader和childheader是 arraylist 。 masterheader默认包含所有标头。我们要在 childheader 中添加nodename,如果两者都匹配,那么我们在结果字符串中添加值。例如
masterheader = [employeename,employeesurname,fathername,mobile,salary]
childheader = [employeename,mobile,salary] //这是基于xml解析。

  for(i = 0; i< masterheader.size() ; i ++)
{
for(j = 0; j< childheader.size(); j ++)
{
if((masterheader [i])。equals(childheader [ j]))
{
//获取该节点的值,+,
}
其他
{
count ++
}
}
if(count == childheader.size()){
//添加到结果字符串+ null +,
}
}


解决方案

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

DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document document = docBuilder.parse(new File(document.xml));

NodeList nodeList = document.getElementsByTagName(*);
for(int i = 0; i< nodeList.getLength(); i ++){
Node node = nodeList.item(i);
if(node.getNodeType()== Node.ELEMENT_NODE){
//使用当前元素执行某些操作
System.out.println(node.getNodeName());
}
}
}

我认为这会有所帮助


I'm trying to read XML(whatever, i don't know it's structure, bcz i'm reading flie from server.), and i searched a lot on SO. but not found anything helpful.

1> i want to retrieve XML's node name, starting from root to end of file, child or child of child untill there's no child or sibling remaining.

2> in similar question, i found that they use tagname to retrieve value of that tag, so i want to pass that node name inside this to get value...

is it possible to do dynamically. or have to follow same structure as in previous questions like statically add tagname to retrieve value.

after doing this in java, i want to create executable jar, and want to use it in mirth.


EDIT i want to find element and it's value such that, in first condition i'm looping my arraylist which i named masterheader which contain all tag name, and in second loop i'm going to find node name from xml, and and inside that two for loop i specified one condition, if both match, then it get node name and it store a value in , separated form, because i want to convert it in csv.

here, masterheader and childheader is arraylist. masterheader contain all header by default. and we gonna add nodename in childheader, nd if both match then we add value in result string. for e.g. masterheader = [employeename, employeesurname, fathername, mobile, salary] and in childheader = [employeename, mobile, salary] //this is based on xml parsing.

for(i=0;i<masterheader.size();i++)
{
    for(j=0;j<childheader.size();j++)
    {
         if((masterheader[i]).equals(childheader[j]))
        {
           //get the value of that node, + ","        
        }
        else
         {
                count++
         }
    }
if(count==childheader.size()){
 //add into result string +null+","  
}
}

解决方案

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("document.xml"));

    NodeList nodeList = document.getElementsByTagName("*");
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            // do something with the current element
            System.out.println(node.getNodeName());
        }
    }
}

i think this will help

这篇关于动态读取java中的xml元素和值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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