8

NodeListを使用してXMLドキュメントオブジェクトを作成する必要があります。誰かplsは私がこれをするのを手伝ってくれますか?これは私のJavaコードです:

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.*; 
import org.w3c.dom.*;

public class ReadFile {

    public static void main(String[] args) {
        String exp = "/configs/markets";
        String path = "testConfig.xml";
        try {
            Document xmlDocument = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(path);
            XPath xPath = XPathFactory.newInstance().newXPath();
            XPathExpression xPathExpression = xPath.compile(exp);
            NodeList nodes = (NodeList)
              xPathExpression.evaluate(xmlDocument,
                                       XPathConstants.NODESET);

        } catch (Exception ex) {
            ex.printStackTrace();
        } 
    }
}

次のようなXMLファイルが必要です。

<configs>
    <markets>   
        <market>
            <name>Real</name>
        </market>
        <market>
            <name>play</name>
        </market>
    </markets>
</configs>

前もって感謝します。

4

2 に答える 2

15

次のようにする必要があります。

  • org.w3c.dom.Document newXmlDocにノードを保存する新しい場所を作成しNodeList
  • 新しいルート要素を作成し、それを追加しますnewXmlDoc
  • 次に、 の各ノードnを にNodeListインポートnし、の子としてnewXmlDoc追加しますnroot

コードは次のとおりです。

public static void main(String[] args) {
    String exp = "/configs/markets/market";
    String path = "src/a/testConfig.xml";
    try {
        Document xmlDocument = DocumentBuilderFactory.newInstance()
                .newDocumentBuilder().parse(path);

        XPath xPath = XPathFactory.newInstance().newXPath();
        XPathExpression xPathExpression = xPath.compile(exp);
        NodeList nodes = (NodeList) xPathExpression.
                evaluate(xmlDocument, XPathConstants.NODESET);

        Document newXmlDocument = DocumentBuilderFactory.newInstance()
                .newDocumentBuilder().newDocument();
        Element root = newXmlDocument.createElement("root");
        newXmlDocument.appendChild(root);
        for (int i = 0; i < nodes.getLength(); i++) {
            Node node = nodes.item(i);
            Node copyNode = newXmlDocument.importNode(node, true);
            root.appendChild(copyNode);
        }

        printTree(newXmlDocument);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

public static void printXmlDocument(Document document) {
    DOMImplementationLS domImplementationLS = 
        (DOMImplementationLS) document.getImplementation();
    LSSerializer lsSerializer = 
        domImplementationLS.createLSSerializer();
    String string = lsSerializer.writeToString(document);
    System.out.println(string);
}

出力は次のとおりです。

<?xml version="1.0" encoding="UTF-16"?>
<root><market>
            <name>Real</name>
        </market><market>
            <name>play</name>
        </market></root>

いくつかのメモ:

  • 単一の要素ではなく、要素をコピーしたいのではないかと思われるため、に変更expしました/configs/markets/marketmarketmarkets
  • については、この回答printXmlDocumentで興味深いコードを使用しました

これが役立つことを願っています。


新しいルート要素を作成したくない場合は、NodeList直接追加できる単一のノード (XML には単一のルート要素が必要であることに注意してください) で構成される元の XPath 式を使用できます。新しい XML ドキュメント。

上記のコードの行にコメントを付けた次のコードを参照してください。

public static void main(String[] args) {
    //String exp = "/configs/markets/market/";
    String exp = "/configs/markets";
    String path = "src/a/testConfig.xml";
    try {
        Document xmlDocument = DocumentBuilderFactory.newInstance()
                .newDocumentBuilder().parse(path);

        XPath xPath = XPathFactory.newInstance().newXPath();
        XPathExpression xPathExpression = xPath.compile(exp);
        NodeList nodes = (NodeList) xPathExpression.
        evaluate(xmlDocument,XPathConstants.NODESET);

        Document newXmlDocument = DocumentBuilderFactory.newInstance()
                .newDocumentBuilder().newDocument();
        //Element root = newXmlDocument.createElement("root");
        //newXmlDocument.appendChild(root);
        for (int i = 0; i < nodes.getLength(); i++) {
            Node node = nodes.item(i);
            Node copyNode = newXmlDocument.importNode(node, true);
            newXmlDocument.appendChild(copyNode);
            //root.appendChild(copyNode);
        }

        printXmlDocument(newXmlDocument);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

これにより、次の出力が得られます。

<?xml version="1.0" encoding="UTF-16"?>
<markets>   
        <market>
            <name>Real</name>
        </market>
        <market>
            <name>play</name>
        </market>
    </markets>
于 2011-04-26T07:50:35.517 に答える
0

DocumentadoptNode()の方法を試すことができます。たぶん、あなたの. で個人にアクセスできます。検索結果を でラップしたい場合は、新しく作成されたおよびでcreateElement() を使用できます。

NodeListNodesnodeList.item(i)

ElementDocumentappendChild()Element

于 2011-04-26T06:54:36.527 に答える