I have a problem in Java with DOM... Here is the XML code :

我在Java中遇到了DOM的问题……这里是XML代码:

<?xml version="1.0" encoding="UTF-8"?>
<bib>
<domain>
    <title>Specifications</title>
    <bib_ref>
        <year>March 2000</year>
        <title>MOF  1.3</title>
        <author>OMG</author>
        <weblink>D:\SALIM\Docs\Specifications\MOF1_3.pdf</weblink>
    </bib_ref>
    <bib_ref>
        <year>August 2002</year>
        <title>IDLto Java LanguageMapping Specification</title>
        <author>OMG</author>
        <weblink>D:\SALIM\Docs\Specifications\IDL2Java.pdf</weblink>
    </bib_ref>
    <bib_ref>
        <year>1999</year>
        <title>XML Metadata Interchange (XMI) Version 1.1</title>
        <author>OMG</author>
        <weblink>D:\SALIM\Docs\Specifications\xmi-1.1.pdf</weblink>
    </bib_ref>
    <bib_ref>
        <year>2002</year>
        <title>XML Metadata Interchange (XMI) Version 2</title>
        <author>"OMG</author>
        <weblink>D:\SALIM\Docs\Specifications\XMI2.pdf</weblink>
    </bib_ref>
    <bib_ref>
        <year>2002</year>
        <title>XMI Version 1Production of XML Schema Specification</title>
        <author>OMG</author>
        <weblink>D:\SALIM\Docs\Specifications\XMI1XSD.pdf</weblink>
    </bib_ref>
    <bib_ref>
        <year>2002</year>
        <title>EDOC</title>
        <author>OMG</author>
        <weblink>D:\SALIM\Docs\Specifications\EDOC02-02-05.pdf</weblink>
    </bib_ref>
</domain>
<domain>
    <title>Theses</title>
    <bib_ref>
        <year>Octobre 2001</year>
        <title>Echanges de Spécifications Hétérogènes et Réparties</title>
        <author>Xavier Blanc</author>
        <weblink>D:\SALIM\Docs\Theses\TheseXavier.pdf</weblink>
    </bib_ref>
    <bib_ref>
        <year>Janvier 2001</year>
        <title>Composition of Object-Oriented Software Design Models</title>
        <author>Siobhan Clarke</author>
        <weblink>D:\SALIM\Docs\Theses\SClarkeThesis.pdf</weblink>
    </bib_ref>
 ......
 ......

After, in Java main function, I call the dispContent function which is just there:

之后,在Java main函数中,我调用了刚才的内容函数:

public void dispContent (Node n) 
{

    String domainName = null;

    // we are in an element node
    if (n instanceof Element) {
        Element e = ((Element) n);

        // domain title
        if (e.getTagName().equals("title") && e.getParentNode().getNodeName().equals("domain")) {
            domainName = e.getTextContent();
            DomaineTemplate(domainName);
        }

        else if (e.getTagName().equals("bib_ref")) {
            NodeList ref = e.getChildNodes();

            for (int i = 0; i < ref.getLength(); i++) {
                Node temp = (Node) ref.item(i);

                if (temp.getNodeType() == Node.ELEMENT_NODE) {
                    if (temp.getNodeType() == org.w3c.dom.Node.TEXT_NODE)
                        continue;

                    out.println(temp.getNodeName() + " : " + temp.getTextContent() + "\n");
                }
            }
        }

        else {
            NodeList sub = n.getChildNodes();
            for(int i=0; (i < sub.getLength()); i++)
                dispContent(sub.item(i));

        }
    }
        /*else if (n instanceof Document) {
        NodeList fils = n.getChildNodes();
        for(int i=0; (i < fils.getLength()); i++) {
            dispContent(fils.item(i));

        }
    }*/
}

The "domaineTemplate" function just displays its parameter! My problem happens when I browse the "bib_ref" tag in Java. For each "bib_ref" loop, it displays all the contents of all "bib_ref" tags... in one line! I want to display only one content (year, title, author and weblink tag) per "bib_ref".

“domaineTemplate”函数只显示它的参数!当我在Java中浏览“bib_ref”标记时,就会出现问题。对于每个“bib_ref”循环,它将显示所有“bib_ref”标记的所有内容…在一行!我只希望显示一个内容(年、标题、作者和weblink标签),每个“bib_ref”。

Here is what it is displaying at the moment when I browse bib_ref :

以下是我浏览bib_ref时显示的内容:

Specifications

规范

year : March 2000 title : MOF 1.3 author : OMG weblink : D:\SALIM\Docs\Specifications\MOF1_3.pdf year : August 2002 title : IDLto Java LanguageMapping Specification author : OMG weblink : D:\SALIM\Docs\Specifications\IDL2Java.pdf year : 1999 title : XML Metadata Interchange (XMI) Version 1.1 author : OMG weblink : D:\SALIM\Docs\Specifications\xmi-1.1.pdf year : 2002 title : XML Metadata Interchange (XMI) Version 2 author : OMG weblink : D:\SALIM\Docs\Specifications\XMI2.pdf year : 2002 title : XMI Version 1Production of XML Schema Specification author : OMG weblink : D:\SALIM\Docs\Specifications\XMI1XSD.pdf year : 2002 title : EDOC author : OMG weblink : D:\SALIM\Docs\Specifications\EDOC02-02-05.pdf

年:2000年3月标题:MOF 1.3作者:OMG weblink: D:\SALIM\Docs\规格\MOF1_3。pdf年:2002年8月的标题:IDLto Java语言编程规范作者:OMG weblink: D:\SALIM\Docs\Specification \IDL2Java。pdf年:1999年标题:XML元数据交换(XMI) 1.1版本作者:OMG weblink: D:\SALIM\Docs\规格\ XMI -1.1。pdf年:2002标题:XML元数据交换(XMI)第2版作者:OMG weblink: D:\SALIM\Docs\文档规范\XMI2。pdf年:2002年标题:XMI版本1 XML模式规范的生产作者:OMG weblink: D:\SALIM\Docs\规格\XMI1XSD。pdf年:2002年标题:EDOC作者:OMG weblink: D:\SALIM\Docs\ specification \EDOC02-02-05.pdf

Theses

论文

year : Octobre 2001 title : Echanges de Sp�cifications H�t�rog�nes et R�parties author : Xavier Blanc weblink : D:\SALIM\Docs\Theses\TheseXavier.pdf year : Janvier 2001 title : Composition of Object-Oriented Software Design Models author : Siobhan Clarke weblink : D:\SALIM\Docs\Theses\SClarkeThesis.pdf year : Juin 2002 title : Contribution � la repr�sentation de processu par des techniques de m�ta mod�lisation author : Erwan Breton weblink : D:\SALIM\Docs\Theses\ErwanBretonThesis.pdf year : Octobre 2000 title : Technique de Mod�lisation et de M�ta mod�lisation author : Richard Lemesle weblink : D:\SALIM\Docs\Theses\RichardLemesle.pdf year : Juillet 2002 title : Utilsation d'agents mobiles pour la construction des services distribu�s author : Siegfried Rouvrais weblink : D:\SALIM\Docs\Theses\theserouvrais.pdf ... ...

2001年:Octobre标题:与德Sp�cifications H�t�罗格�nes et R�方作者:泽维尔布兰科连接:D:\萨利姆\ Docs \ \ TheseXavier论文。pdf年:Janvier 2001 title:面向对象软件设计模型的作者:Siobhan Clarke weblink: D:\SALIM\Docs\ SClarkeThesis。pdf:Juin 2002标题:贡献repr��la sentation de processu par des技术de m�ta mod�lisation作者:该款布列塔连接:D:\萨利姆\ Docs \ \ ErwanBretonThesis论文。pdf:Octobre 2000标题:技术de Mod��lisation et de米ta Mod�lisation作者:理查德Lemesle连接:D:\萨利姆\ Docs \ \ RichardLemesle论文。pdf:Juillet 2002标题:Utilsation d经纪人手机倒拉建筑des服务distribu�年代作者:齐格弗里德Rouvrais连接:d:\萨利姆\ Docs \ \ theserouvrais论文。pdf……

Can you help me ? I'm just a beginner in xml with java and I'm searching some solution for about 3 hours... Thank's a lot!

你能帮我吗?我只是一个使用java的xml初学者,我花了大约3个小时搜索一些解决方案……谢谢很多!

2 个解决方案

#1


4

I called dispContent(doc.getFirstChild()); where doc is the Document with your given xml file contents.

我叫dispContent(doc.getFirstChild());doc是包含给定xml文件内容的文档。

Assumptions: out.println() is System.out.println() and DomaineTemplate(domainName); adds a newline (based on your output provided)

假设:out.println()是System.out.println()和DomaineTemplate(domainName);添加一条换行符(基于您提供的输出)

I get the following print out in the console:

我在控制台打印如下:

Specifications

year : March 2000

title : MOF  1.3

author : OMG

weblink : D:\SALIM\Docs\Specifications\MOF1_3.pdf

year : August 2002

title : IDLto Java LanguageMapping Specification

author : OMG

weblink : D:\SALIM\Docs\Specifications\IDL2Java.pdf

year : 1999

title : XML Metadata Interchange (XMI) Version 1.1

author : OMG

weblink : D:\SALIM\Docs\Specifications\xmi-1.1.pdf

year : 2002

title : XML Metadata Interchange (XMI) Version 2

author : "OMG

weblink : D:\SALIM\Docs\Specifications\XMI2.pdf

year : 2002

title : XMI Version 1Production of XML Schema Specification

author : OMG

weblink : D:\SALIM\Docs\Specifications\XMI1XSD.pdf

year : 2002

title : EDOC

author : OMG

weblink : D:\SALIM\Docs\Specifications\EDOC02-02-05.pdf

Theses

year : Octobre 2001

title : Echanges de Spécifications Hétérogènes et Réparties

author : Xavier Blanc

weblink : D:\SALIM\Docs\Theses\TheseXavier.pdf

year : Janvier 2001

title : Composition of Object-Oriented Software Design Models

author : Siobhan Clarke

weblink : D:\SALIM\Docs\Theses\SClarkeThesis.pdf

If you're having an issue with "\n" creating a new line, you can try using what the System uses:

如果你对“\n”创建新行有问题,你可以尝试使用系统使用的:

public static final String NEW_LINE = System.getProperty("line.separator");

If you don't want the new lines between each line of the "bib_ref" node's children print outs, change:

如果不希望在“bib_ref”节点的子节点的每一行之间打印出新的行,请更改:

else if (e.getTagName().equals("bib_ref")) {
    NodeList ref = e.getChildNodes();

        for (int i = 0; i < ref.getLength(); i++) {
            Node temp = (Node) ref.item(i);

            if (temp.getNodeType() == Node.ELEMENT_NODE) {
                if (temp.getNodeType() == org.w3c.dom.Node.TEXT_NODE)
                    continue;

                 out.println(temp.getNodeName() + " : " + temp.getTextContent() + "\n");
            }
        }
}

to:

:

else if (e.getTagName().equals("bib_ref")) {
    NodeList ref = e.getChildNodes();

        for (int i = 0; i < ref.getLength(); i++) {
            Node temp = (Node) ref.item(i);

            if (temp.getNodeType() == Node.ELEMENT_NODE) {
                if (temp.getNodeType() == org.w3c.dom.Node.TEXT_NODE)
                    continue;

                 // Removed "\n":
                 out.println(temp.getNodeName() + " : " + temp.getTextContent());
            }
        }

        // Added out.println();
        out.println();
}

Results:

结果:

Specifications

year : March 2000
title : MOF  1.3
author : OMG
weblink : D:\SALIM\Docs\Specifications\MOF1_3.pdf

year : August 2002
title : IDLto Java LanguageMapping Specification
author : OMG
weblink : D:\SALIM\Docs\Specifications\IDL2Java.pdf

year : 1999
title : XML Metadata Interchange (XMI) Version 1.1
author : OMG
weblink : D:\SALIM\Docs\Specifications\xmi-1.1.pdf

year : 2002
title : XML Metadata Interchange (XMI) Version 2
author : "OMG
weblink : D:\SALIM\Docs\Specifications\XMI2.pdf

year : 2002
title : XMI Version 1Production of XML Schema Specification
author : OMG
weblink : D:\SALIM\Docs\Specifications\XMI1XSD.pdf

year : 2002
title : EDOC
author : OMG
weblink : D:\SALIM\Docs\Specifications\EDOC02-02-05.pdf

Theses

year : Octobre 2001
title : Echanges de Spécifications Hétérogènes et Réparties 
author : Xavier Blanc
weblink : D:\SALIM\Docs\Theses\TheseXavier.pdf

year : Janvier 2001
title : Composition of Object-Oriented Software Design Models
author : Siobhan Clarke
weblink : D:\SALIM\Docs\Theses\SClarkeThesis.pdf

更多相关文章

  1. 如何在离子框架中显示图像标题?
  2. 从浏览器中删除发送到服务器的标题。
  3. 如果作者是Post的作者那么做点什么?
  4. 维基百科,如在PHP中列出标题的索引
  5. 如果有多个作者[重复],我怎么能阻止PHP显示该书两次
  6. WebKit "拒绝设置不安全的标题'内容长度' "
  7. 拒绝获取不安全标题“位置”
  8. 为什么我收到此错误:“未捕获的TypeError:无法读取未定义的属性'标
  9. IE:令人难以置信的跳跃表标题

随机推荐

  1. 关于android常用的按下高亮的展示
  2. Android 学习1----控件的学习
  3. Android的图形显示原理(GDI)一
  4. Android 事件分发机制源码
  5. Android(安卓)ColorMatrix类图像颜色处理
  6. [置顶] Android曙光集群发来的邀请函
  7. Android Service 服务详细讲解
  8. Android应用程序与SurfaceFlinger服务的
  9. 编译Android最新源码(090508)
  10. android背景选择器selector用法、自定义B