2016-03-20 3 views
1

Ich brauche Wert wie "Symbol" ect. aus XML-Datei und an die Liste senden.Wie erhalten XML-Wert von Unziped Datei

Vorerst mein Code sieht wie folgt aus:

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

Ich möchte wissen, wie kann ich dies und takevalue „xxx“ und „zzzz“ contunue auf eine andere Liste zu setzen, weil ich einen gewissen Wert Conferencier brauchen .

XML sieht wie folgt aus:

<?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: Hallo, ich hoffe, dass meine Lösung sehr nützlich sein für jemanden

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

:) würde ich mich freuen, wenn ich jemand durch mein Code helfen :)

+0

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

Antwort

0

ein Verfahren wie das hinzufügen alle Elemente abzurufen, die einen bestimmten Pfad Ausdruck entsprechen:

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

andere Methode hinzufügen Dokument aus einer XML-Eingabe zu bauen:

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

Und dann setzen sie alle zusammen:

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

Ich hoffe, ich kopiert und diese korrekt eingefügt. Ich habe dieses von a working class in einem Open-Source-Projekt von mir gezogen und es ein bisschen aufgeräumt, um Ihre spezifische Frage zu beantworten.

+0

Wirklich danke für Ihre Wiederholung, ich habe zwei Probleme, was ich in buildDoc wie InputStrem und secound erhalten muss Text text = node.getChildNodes(). item (0); <- Ich habe einen Fehler hier ist etwas mit der Bibliothek. –