一つの方法:
echo "<Request>SayHello</Request><Response>Wave</Response>" | perl -ne 'print "Request: $1 Response: $2\n" if /<Request>(.*?)<\/Request><Response>(.*?)<\/Response>/'
EDIT:
[OK]を、ビットより一般的な任意のタグをキャッチし、印刷するには(ファイルx.plに入れます):
#!/usr/bin/perl -w
while (<STDIN>) {
while ($_ =~ /<(.*?)>(.*?)<\/\1>/g) {
print ($1 . ": " . $2 . "\n");
}
}
使用例は次のようになります。
echo "<Request>Hello</Request><Response>Goodbye</Response><Other>Foo</Other>"|./x.pl
EDIT2:
import javax.xml.parsers.*;
import javax.xml.xpath.*;
import org.w3c.dom.*;
public class Example {
Example(String xml) {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = dbf.newDocumentBuilder();
Document doc = builder.parse(xml);
XPath xpath = XPathFactory.newInstance().newXPath();
XPathExpresion expr = xpath.compile("/some/xml/path/text()");
NodeList nl = (NodeList) xpath.evaluate(doc, XPathConstants.NODE_LIST);
System.out.println("Node value: " + nl.items(0).getNodeValue());
}
}
こんにちはケビン、うまくええそれは働いていたが、何I:
そして、ここではあなたがXPath式に基づいて好き引き出すことができますJavaの持つもう一つの方法ですXML内の各タグ名を取って、それ自体の名前にかかわらずその値とともに表示することが必要です。それは次のようにxmlをフォーマットするのと似ています:リクエスト:タグ名にかかわらず、各タグを取り、それを置く一般的な機能のように、こんにちはと言ってください。 – GRIM
@GRIM:私の更新された例を見てください。ネストされたタグでは機能しないことに注意してください。あなたが興味を持っているかどうかは分かりません。 – Kevin
ありがとうKevin !!! – GRIM