Java 如何从未压缩的文件中获取XML值

Java 如何从未压缩的文件中获取XML值,java,xml,iteration,Java,Xml,Iteration,我需要得到像“符号”之类的值。从xml文件发送到列表 目前,我的代码如下所示: Scanner sc = null; byte[] buff = new byte[1 << 13]; List<String> question2 = new ArrayList<String>(); question2 = <MetodToGetFile>(sc,fileListQ); for ( String strLista :

我需要得到像“符号”之类的值。从xml文件发送到列表

目前,我的代码如下所示:

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

如果我能通过我的代码帮助某人,我会很高兴:)

添加这样的方法来检索与给定路径表达式匹配的所有元素:

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;
}
然后把它们放在一起:

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();
}
InputSource is=newinputsource(newstringreader(“…您的XML字符串在此”);
文档文档=构建文档(is);
列表节点=getNodes(doc,“/Name/Group/Package/Attribute”);
用于(节点:节点){
//对于元素的文本体,首先获取其嵌套的文本子级
Text Text=node.getChildNodes().item(0);
//然后询问该文本子项的值
字符串内容=node.getNodeValue();
}

我希望我复制并粘贴正确。我从我的一个开源项目中提取了这个,并对它进行了一些清理,以回答您的特定问题。

添加这样一个方法来检索与给定路径表达式匹配的所有元素:

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;
}
然后把它们放在一起:

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();
}
InputSource is=newinputsource(newstringreader(“…您的XML字符串在此”);
文档文档=构建文档(is);
列表节点=getNodes(doc,“/Name/Group/Package/Attribute”);
用于(节点:节点){
//对于元素的文本体,首先获取其嵌套的文本子级
Text Text=node.getChildNodes().item(0);
//然后询问该文本子项的值
字符串内容=node.getNodeValue();
}

我希望我复制并粘贴正确。我从我的一个开源项目中提取了这个,并对它进行了一些清理,以回答您的特定问题。

非常感谢您的重播,我有两个问题需要在buildDoc中获得,如InputStrem和secound Text Text=node.getChildNodes()。项(0);非常感谢您的重播,我有两个问题需要在buildDoc中获得,如InputStrem和secound Text=node.getChildNodes().item(0);