2016-03-20 20 views
1

"Symbol" gibi bir değer almam gerekiyor. xml dosyasından listeye gönderin.Unziped dosyasından XML değeri nasıl alınır?

Şimdilik benim kod şöyle görünür: i bazı değer sunuculuk gerektiğinden

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); 
    } 
    } 
    } 
    } 
} 

Ben, ben başka bir listeye koymak için bu ve takevalue "xxx" ve "zzzz" contunue nasıl bilmek istiyorum .

<?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:

XML şöyle Merhaba ben birileri

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(); 
       } 

benim koduyla biri yardımcı olacaktır eğer mutlu olurdu :) benim çözüm yararlı olacağını umuyoruz :)

+0

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

cevap

0

Verilen bir Yol ifadesine uyan öğelerin tümünü almak için buna benzer bir yöntem ekleyin:

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; 
} 

bir XML girişten Belge oluşturmak için başka bir yöntem ekleyin:

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; 
} 

Ve sonra hep birlikte koymak:

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(); 
} 

ben doğru biçimde kopyalanarak bu yapıştırılan umuyoruz. Bunu bir açık kaynak projemde a working class'dan çektim ve belirli bir soruyu cevaplamak için biraz temizledim.

+0

Gerçekten teşekkürler tekrar çalıştırmak için, ben InputStrem ve secound Metin metin gibi buildDoc almak için gerekenleri iki sorun var = . node.getChildNodes() ürün (0); <- Burada hatam kütüphaneyle ilgili bir şey var. –

İlgili konular