Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/400.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 删除xml文件中字符串前后不需要的字符串_Java_Xml_Xml Parsing - Fatal编程技术网

Java 删除xml文件中字符串前后不需要的字符串

Java 删除xml文件中字符串前后不需要的字符串,java,xml,xml-parsing,Java,Xml,Xml Parsing,xml文件内容 <distributionChannels><distributionChannel type="Wap" id="1"><contentChannelRefs> <contentChannelRef id="2"><categories><category link="http://images/11.gif" id="1"><names><name lang="de">Top D

xml文件内容

<distributionChannels><distributionChannel type="Wap" id="1"><contentChannelRefs>
<contentChannelRef id="2"><categories><category 
link="http://images/11.gif" id="1"><names><name lang="de">Top Downloads</name><name
lang="ww">Tops</name></names></category></categories></contentChannelRef>
</contentChannelRefs></distributionChannel>  
</distributionChannels>

顶部下载站
如何从xml文件中删除我正在读取的不需要的内容,输出应如下所示:

<category link="http://images/11.gif" id="1"><names><name lang="de">Top Downloads</name><name lang="ww">Tops</name></names></category>
顶部下载站点

可靠的解决方案-使用XML解析器。简单的解决办法是

s = s.substring(s.indexOf("<categories>"), s.indexOf("</categories>") + 13);
s=s.substring(s.indexOf(“”),s.indexOf(“”+13);
如果您想逐个阅读类别,请使用正则表达式

    Matcher m = Pattern.compile("<category.*?>.*?</category>").matcher(xml);
    for(int i = 0; m.find(); i++) {
        System.out.println(m.group());
    }
Matcher m=Pattern.compile(“.*”).Matcher(xml);
对于(int i=0;m.find();i++){
System.out.println(m.group());
}

可靠的解决方案-使用XML解析器。简单的解决办法是

s = s.substring(s.indexOf("<categories>"), s.indexOf("</categories>") + 13);
s=s.substring(s.indexOf(“”),s.indexOf(“”+13);
如果您想逐个阅读类别,请使用正则表达式

    Matcher m = Pattern.compile("<category.*?>.*?</category>").matcher(xml);
    for(int i = 0; m.find(); i++) {
        System.out.println(m.group());
    }
Matcher m=Pattern.compile(“.*”).Matcher(xml);
对于(int i=0;m.find();i++){
System.out.println(m.group());
}

不建议使用XML进行模式匹配。使用解析器获取节点并相应地管理它们。如果您对打印它们感兴趣,我已经提供了打印节点的代码

public static void main(String[] args)
        throws ParserConfigurationException, SAXException,
        IOException, XPathExpressionException {
    DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
    domFactory.setNamespaceAware(true);
    DocumentBuilder builder = domFactory.newDocumentBuilder();
    Document doc = builder.parse(new InputSource(new StringReader(s)));

    XPathFactory factory = XPathFactory.newInstance();
    XPath xpath = factory.newXPath();
    XPathExpression expr
            = xpath.compile("//categories//category");

    Object result = expr.evaluate(doc, XPathConstants.NODESET);
    NodeList nodes = (NodeList) result;
    //This is where you are printing things. You can handle differently if
    //you would like.
    for (int i = 0; i < nodes.getLength(); i++) {
        System.out.println(nodeToString(nodes.item(i)));
    }
}

private static String nodeToString(Node node) {
    StringWriter sw = new StringWriter();
    try {
        Transformer t = TransformerFactory.newInstance().newTransformer();
        t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        t.setOutputProperty(OutputKeys.INDENT, "yes");
        t.transform(new DOMSource(node), new StreamResult(sw));
    } catch (TransformerException te) {
        te.printStackTrace();
    }
    return sw.toString();
}
publicstaticvoidmain(字符串[]args)
抛出ParserConfiguration异常、SAXException、,
IOException,XPathExpressionException{
DocumentBuilderFactory domFactory=DocumentBuilderFactory.newInstance();
domFactory.setNamespaceAware(true);
DocumentBuilder=domFactory.newDocumentBuilder();
Document doc=builder.parse(新的InputSource(新的StringReader));
XPathFactory=XPathFactory.newInstance();
XPath=factory.newXPath();
XPathExpression
=xpath.compile(“//categories//category”);
Object result=expr.evaluate(doc,XPathConstants.NODESET);
节点列表节点=(节点列表)结果;
//这是你打印东西的地方。如果
//你会喜欢的。
对于(int i=0;i
不建议使用XML进行模式匹配。使用解析器获取节点并相应地管理它们。如果您对打印它们感兴趣,我已经提供了打印节点的代码

public static void main(String[] args)
        throws ParserConfigurationException, SAXException,
        IOException, XPathExpressionException {
    DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
    domFactory.setNamespaceAware(true);
    DocumentBuilder builder = domFactory.newDocumentBuilder();
    Document doc = builder.parse(new InputSource(new StringReader(s)));

    XPathFactory factory = XPathFactory.newInstance();
    XPath xpath = factory.newXPath();
    XPathExpression expr
            = xpath.compile("//categories//category");

    Object result = expr.evaluate(doc, XPathConstants.NODESET);
    NodeList nodes = (NodeList) result;
    //This is where you are printing things. You can handle differently if
    //you would like.
    for (int i = 0; i < nodes.getLength(); i++) {
        System.out.println(nodeToString(nodes.item(i)));
    }
}

private static String nodeToString(Node node) {
    StringWriter sw = new StringWriter();
    try {
        Transformer t = TransformerFactory.newInstance().newTransformer();
        t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        t.setOutputProperty(OutputKeys.INDENT, "yes");
        t.transform(new DOMSource(node), new StreamResult(sw));
    } catch (TransformerException te) {
        te.printStackTrace();
    }
    return sw.toString();
}
publicstaticvoidmain(字符串[]args)
抛出ParserConfiguration异常、SAXException、,
IOException,XPathExpressionException{
DocumentBuilderFactory domFactory=DocumentBuilderFactory.newInstance();
domFactory.setNamespaceAware(true);
DocumentBuilder=domFactory.newDocumentBuilder();
Document doc=builder.parse(新的InputSource(新的StringReader));
XPathFactory=XPathFactory.newInstance();
XPath=factory.newXPath();
XPathExpression
=xpath.compile(“//categories//category”);
Object result=expr.evaluate(doc,XPathConstants.NODESET);
节点列表节点=(节点列表)结果;
//这是你打印东西的地方。如果
//你会喜欢的。
对于(int i=0;i
谢谢Dorofev,如果我在同一个字符串中有多个字符串,我该如何解决…谢谢,我的意思是我只想提取,假设我有100个,我将在第一个文件中写入50个,然后在第二个文件中写入50个。好的,试试新版本,注意它可以轻松提取attr值和标记内容,只需稍加修改。谢谢Dorofev,如果我在同一个字符串中有多个,我该如何解决…谢谢,我的意思是我只想提取,假设我有100个,我将50个写入第一个文件,下50个写入第二个文件。好的,试试新版本,注意它可以轻松提取属性值和标记内容,只需稍加修改