5

私はパーサーXmlDocumentで作成されたJavaを持っています。Weblogic XmlDocument

この中のタグの内容を自分のデータに置き換えるXMLDocumentか、タグがなければ挿入したいです。

<customdata>
   <tag1 />
   <tag2>mfkdslmlfkm</tag2>
   <location />
   <tag3 />
</customdata>

たとえば、場所タグに URL を挿入したいとします。

<location>http://something</location>

それ以外の場合は XML をそのままにしておきます。

現在、私は次を使用していXMLCursorます:

    XmlObject xmlobj = XmlObject.Factory.parse(a.getCustomData(), options);
    XmlCursor xmlcur = xmlobj.newCursor();

    while (xmlcur.hasNextToken()) {
      boolean found = false;
      if (xmlcur.isStart() && "schema-location".equals(xmlcur.getName().toString())) {
        xmlcur.setTextValue("http://replaced");
        System.out.println("replaced");
        found = true;
      } else if (xmlcur.isStart() && "customdata".equals(xmlcur.getName().toString())) {
        xmlcur.push();
      } else if (xmlcur.isEnddoc()) {
        if (!found) {
          xmlcur.pop();
          xmlcur.toEndToken();
          xmlcur.insertElementWithText("schema-location", "http://inserted");
          System.out.println("inserted");
        }

      }
      xmlcur.toNextToken();
    }

にはメソッドがあるため、これを行うための「迅速な」xquery方法を見つけようとしましたが、それほど簡単ではありませんでした。XmlDocumentexecQuery

誰かがこれよりも良い方法を持っていますか? 少し手の込んだようです。

4

4 に答える 4

5

XPath ベースのアプローチはどうですか? ロジックが非常に理解しやすいため、このアプローチが気に入っています。コードはほぼ自己文書化されています。

xml ドキュメントが org.w3c.dom.Document オブジェクトとして利用できる場合 (ほとんどのパーサーが返すように)、次のようなことができます。

// get the list of customdata nodes
NodeList customDataNodeSet = findNodes(document, "//customdata" );

for (int i=0 ; i < customDataNodeSet.getLength() ; i++) {
  Node customDataNode = customDataNodeSet.item( i );

  // get the location nodes (if any) within this one customdata node
  NodeList locationNodeSet = findNodes(customDataNode, "location" );

  if (locationNodeSet.getLength() > 0) {
    // replace
    locationNodeSet.item( 0 ).setTextContent( "http://stackoverflow.com/" );
  }
  else {
    // insert
    Element newLocationNode = document.createElement( "location" );
    newLocationNode.setTextContent("http://stackoverflow.com/" );
    customDataNode.appendChild( newLocationNode );
  }
}

XPath 検索を行うヘルパー メソッド findNodes を次に示します。

private NodeList findNodes( Object obj, String xPathString )
  throws XPathExpressionException {

  XPath xPath = XPathFactory.newInstance().newXPath();
  XPathExpression expression = xPath.compile( xPathString );
  return (NodeList) expression.evaluate( obj, XPathConstants.NODESET );
}
于 2008-08-19T11:31:10.433 に答える
2

オブジェクト指向のアプローチはどうですか?XMLをオブジェクトに逆シリアル化し、オブジェクトに場所の値を設定してから、XMLにシリアル化することができます。

XStreamはこれを本当に簡単にします。

たとえば、メインオブジェクトを定義します。このオブジェクトはCustomDataです(例を単純にするためにパブリックフィールドを使用しています)。

public class CustomData {
  public String tag1;
  public String tag2;
  public String location;
  public String tag3;
}

次に、XStreamを初期化します。

XStream xstream = new XStream();
// if you need to output the main tag in lowercase, use the following line
xstream.alias("customdata", CustomData.class);  

これで、XMLからオブジェクトを作成し、オブジェクトに場所フィールドを設定して、XMLを再生成できます。

CustomData d = (CustomData)xstream.fromXML(xml);
d.location = "http://stackoverflow.com";
xml = xstream.toXML(d);

それはどのように聞こえますか?

于 2008-08-19T10:45:20.620 に答える
1

スキーマがわからない場合、XStream ソリューションはおそらく適切ではありません。少なくとも XStream は現在あなたのレーダーに乗っています。将来的には便利になるかもしれません!

于 2008-08-19T15:15:36.267 に答える
0

あなたはこれを行うことができるはずですquery

試す

 fn:replace(string,pattern,replace)

私はxqueryを初めて使用し、それを使用するのは面倒なクエリ言語であることがわかりましたが、最初の学習曲線を乗り越えれば、静かに機能します。

私はまだ同じくらい効率的なもっと簡単な方法があったらいいのにと思いますか?

于 2008-08-19T10:38:08.807 に答える