0

私のXMLファイルは次のようになります。

    <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE pointList SYSTEM "point.dtd">
<pointList>
<point unit="mm">
<x>2</x>
<y>3</y>
</point>

<point unit="cm">
<x>9</x>
<y>3</y>
</point>

<point unit="px">
<x>4</x>
<y>7</y>
</point>

</pointList>

タグポイントの属性を取得しようとすると:

import java.io.File;
import java.io.IOException;


import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;


public class TryXml {
    public static void main (String [] args) {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = null;
        Document doc = null;
        try {
            builder = factory.newDocumentBuilder();
        }
        catch (ParserConfigurationException e){
            System.out.println(e.getMessage());
        }
        File f = new File("p1.xml");
        try {
            doc=builder.parse(f);
        }
        catch (SAXException e){
            e.printStackTrace();
        }
        catch (IOException e){
            e.printStackTrace();
        }

        Element root = doc.getDocumentElement();
        System.out.println(root.getTagName());

        System.out.println("****************");


        NodeList nList = root.getChildNodes();
        for (int i=0;i<nList.getLength();i++) {
        if(nList.item(i) instanceof Element)

        System.out.println(nList.item(i).getAttributes());
    }

    }
    }

私が得るのは住所のようなものだけです:

com.sun.org.apache.xerces.internal.dom.AttributeMap@3214512e
com.sun.org.apache.xerces.internal.dom.AttributeMap@53ddbcb1
com.sun.org.apache.xerces.internal.dom.AttributeMap@28f337b

ポイントの属性や他の内部タグを取得する方法について誰かが私にヒントを与えることができますか?

4

4 に答える 4

1

printlnを次のように置き換えることができます。

System.out.println(((Element) nList.item(i)).getAttribute("unit"));

これにより、現在の要素の「ユニット」属性が得られます。

于 2012-05-03T12:41:34.260 に答える
0

使用する、

"要素ルート=doc.getElementByTagName(" pointList ");"

それ以外の、

"要素ルート=doc.getDocumentElement();"

于 2012-05-03T12:42:48.810 に答える
0

Element.getAttributes()その中のすべての属性のリストを提供しますElement。属性の名前が事前にわかっていて、その値を取得したい場合は、Element.getAttribute(String)代わりにを使用してください。

子要素を取得する必要がある場合は、を使用しますElement.getChildNodes()Element、、より具体的には、内のテキストを取得するにはNode、を使用しますgetNodeValue()

例えば:

        NodeList nList = root.getChildNodes();
        String out = "";
        for (int i=0;i<nList.getLength();i++) {
            // Assuming all first-level tags are <point>
            Element point = (Element) nList.item(i);
            String unit = point.getAttribute("unit");
            out += "point ";
            for (int y=0;y<point.getChildNodes().getLength();y++){
                Node child = point.getChildNodes().item(y);
                // String nodeName = child.getNodeName();
                String nodeValue = child.getNodeValue();
                out += nodeValue;
            }
        out += unit;
        out += ", ";
        }

出力しますpoint 23mm, point 93cm, point 47px,

于 2012-05-03T12:43:44.587 に答える
0

javaでxml属性を取得するこの投稿は、あなたが達成しようとしていることを実行します。それは通常のxml解析と操作を教えます。また、属性を取得します。

幸運を!

于 2012-05-03T13:04:49.457 に答える