2016-03-20 12 views
1

「シンボル」などの値を取得する必要があります。 xmlファイルから送信し、リストに送信します。UnzipedファイルからXML値を取得する方法

は、今の私のコードは次のようになります。私はいくつかの値をCOMPEREする必要があるため

Scanner sc = null; 

    byte[] buff = new byte[1 << 13]; 
    List<String> question2 = new ArrayList<String>(); 
    question2 = <MetodToGetFile>(sc,fileListQ); 
    for (String strLista : question2){ 
    ByteArrayInputStream in = new ByteArrayInputStream(strLista.getBytes()); 
    try(InputStream reader = Base64.getMimeDecoder().wrap(in)){ 
    try (GZIPInputStream gis = new GZIPInputStream(reader)) { 
    try (ByteArrayOutputStream out = new ByteArrayOutputStream()){ 
      int readGis = 0; 
      while ((readGis = gis.read(buff)) > 0) 
       out.write(buff, 0, readGis); 
      byte[] buffer = out.toByteArray(); 
      String s2 = new String(buffer); 
    } 
    } 
    } 
    } 
} 

は、私は、私は別のリストに入れて、これとtakevalue「XXX」と「ZZZZ」をcontunueできる方法を知りたいです。

XMLは次のようになります。

<?xml version="1.0" encoding="utf-8"?> 
<Name Name="some value"> 
<Group Names="some value"> 
<Package Guid="{7777-7777-7777-7777-7777}"> 
    <Attribute Typ="" Name="Symbol">xxx</Attribute> 
    <Attribute Type="" Name="Surname">xxx</Attribute> 
    <Attribute Type="Address" Name="Name">zzzz</Attribute> 
    <Attribute Type="Address" Name="Country">zzzz</Attribute> 
</Package> 

EDIT:こんにちは、私は私のコードで誰かを助ける場合、私は幸せになる私の解決策は、誰か:)

try{ 
     //Get is(inputSource with xml in s2(xml string value from stream) 
       InputSource is = new InputSource(new StringReader(s2)); 

       DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
       DocumentBuilder db = dbf.newDocumentBuilder(); 
       Document doc = db.parse(is); 
       XPathFactory xpf = XPathFactory.newInstance(); 
       XPath xpath = xpf.newXPath(); 
       //Get "some value" from attribut Name 
       String name= (String) xpath.evaluate("/Name/@Name", doc, XPathConstants.STRING); 
       //Get "guid" from attribute guid 
       String guid= (String) xpath.evaluate("/Name/Group/Package/@Guid", doc, XPathConstants.STRING); 
       //Get element xxx by tag value Symbol 
       String symbol= xpath.evaluate("/Name/Group/Package/Attribute[@Name=\"Symbol\"]", doc.getDocumentElement()); 
       System.out.println(name); 
       System.out.println(guid); 
       System.out.println(symbol); 
       }catch(Exception e){ 
        e.printStackTrace(); 
       } 

のために役に立つことを願ってい:)

+0

https://docs.oracle.com/javase/tutorial/jaxp/xslt/xpath.html –

答えて

0

このようなメソッドを追加すると、指定したPath式に一致するすべての要素を取得できます。

public List<Node> getNodes(Node sourceNode, String xpathExpresion) throws XPathExpressionException { 
    // You could cache/reuse xpath for better performance 
    XPath xpath = XPathFactory.newInstance().newXPath(); 
    NodeList nodes = (NodeList) xpath.evaluate(xpathExpresion,sourceNode,XPathConstants.NODESET); 
    ArrayList<Node> list = new ArrayList<Node>(); 
    for(int i = 0; i < nodes.getLength(); i++) { 
     Node node = nodes.item(i); 
     list.add(node); 
    } 
    return list; 
} 

XML入力から文書を構築するために、別のメソッドを追加します。

public Document buildDoc(InputStream is) throws Exception { 
    DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance(); 
    DocumentBuilder parser = fact.newDocumentBuilder(); 
    Document newDoc = parser.parse(is); 
    newDoc.normalize(); 
    is.close(); 
    return newDoc; 
} 

をそしてそれをすべて一緒に置く:

InputSource is = new InputSource(new StringReader("... your XML string here")); 
Document doc = buildDoc(is); 
List<Node> nodes = getNodes(doc, "/Name/Group/Package/Attribute"); 
for (Node node: nodes) { 
    // for the text body of an element, first get its nested Text child 
    Text text = node.getChildNodes().item(0); 
    // Then ask that Text child for it's value 
    String content = node.getNodeValue(); 
} 

私はコピーしてこれを正しく貼り付け願っています。私はこれをオープンソースプロジェクトのa working classから取り出し、あなたの特定の質問に答えるためにちょっときれいにしました。

+0

本当に感謝あなたのリプレイのために、私はInputStremとsecound テキストテキストなどのbuildDocに取得するために必要なものの二つの問題を持っています= node.getChildNodes()。item(0); < - 私はここでエラーがあるライブラリと何かがあります。 –

関連する問題