XmlDocument 内の XML タグを挿入/置換する
質問
私は持っています XmlDocument
Javaでは、 Weblogic XmlDocument
パーサー。
このタグの内容を置き換えたい XMLDocument
自分のデータを使用するか、タグが存在しない場合は挿入します。
<customdata>
<tag1 />
<tag2>mfkdslmlfkm</tag2>
<location />
<tag3 />
</customdata>
たとえば、location タグに 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
これを行う方法 XmlDocument
持っています execQuery
方法ですが、それほど簡単ではありませんでした。
これより良い方法がある人はいますか?少し手の込んだようです。
解決
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 );
}
他のヒント
オブジェクト指向のアプローチはどうでしょうか?XML をオブジェクトに逆シリアル化して、そのオブジェクトに位置の値を設定してから、XML にシリアル化して戻すことができます。
Xストリーム これは本当に簡単です。
たとえば、メイン オブジェクトを定義します。この場合、これは 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 からオブジェクトを構築し、そのオブジェクトに location フィールドを設定して、XML を再生成できるようになりました。
CustomData d = (CustomData)xstream.fromXML(xml);
d.location = "http://stackoverflow.com";
xml = xstream.toXML(d);
それはどう聞こえますか?
スキーマがわからない場合、XStream ソリューションはおそらく適していません。少なくとも XStream は今注目されており、将来役立つかもしれません。
これは次のようにできるはずです query
試す
fn:replace(string,pattern,replace)
私自身、xquery は初めてで、扱いにくいクエリ言語だと感じていますが、最初の学習曲線を乗り越えれば、静かにうまく動作します。
同じくらい効率的で簡単な方法があればいいのにと思いますか?