Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/321.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.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 读取并替换xsl fo文件中的标记_Java_Xml_Xsl Fo - Fatal编程技术网

Java 读取并替换xsl fo文件中的标记

Java 读取并替换xsl fo文件中的标记,java,xml,xsl-fo,Java,Xml,Xsl Fo,我需要在java的.fo文件中读取、查找标记并替换它。请帮我弄清楚怎么做?我读过一些主题,但我是java新手,有一些问题。。。对于这个文件,我必须找到标记并在其中替换url=另一个路径 <fo:block font-weight="bold" space-before.optimum="12pt" space-after.optimum="12pt" padding="0.1in" border="thin solid black"> The manufacturer

我需要在java的.fo文件中读取、查找标记并替换它。请帮我弄清楚怎么做?我读过一些主题,但我是java新手,有一些问题。。。对于这个文件,我必须找到标记并在其中替换url=另一个路径

 <fo:block font-weight="bold" space-before.optimum="12pt" space-after.optimum="12pt" padding="0.1in" border="thin solid black">
      The manufacturer declines every liability with regard to any direct or 
      consequential damage caused by the equipment to you, your body parts, 
      your personal belongings, your domestic animals e/o beloved relatives. 
      Use this equipment at your own risk, and let God protect your fingers! 
    </fo:block>

    <fo:block space-before.optimum="6pt">Have fun with our stuff!</fo:block>

  </fo:block><fo:block id="d0e81" text-align="justify" font="11pt Times" line-height="1.3" space-before.minimum="18pt" space-before.conditionality="retain">
    <fo:block font="bold 14pt Helvetica" keep-together.within-column="always" keep-with-next.within-column="always" space-before.minimum="6pt" space-before.optimum="12pt" space-before.conditionality="retain" space-after.optimum="3pt" background-color="silver" padding="3pt" border-top="thin solid black" border-bottom="thin solid black">B.  Unpacking &amp; Installing </fo:block>
    <fo:block space-before.optimum="6pt"> 
      The universal hammer comes shipped in a <fo:wrapper font-style="italic" color="blue" rx:key="carton box">carton box</fo:wrapper> 

  (see Fig. 1)
.
    </fo:block><fo:block margin="3pt" border="thin ridge silver" padding="3pt" space-before.optimum="6pt" space-after.optimum="6pt" text-align="center" font-style="italic" font-family="Helvetica" keep-together.within-column="always"><fo:block text-align="center">
    <fo:external-graphic src="url('Images/box.jpg')"/>
    </fo:block>
      Fig. 1.
      Shipping Box.</fo:block> 

创建一个DOM,然后获取名为fo:externalgraphic的所有元素。然后,您可以将src属性更改为您喜欢的任何属性。以下是一些示例代码,可以帮助您开始:

//parse the xml file
Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new File("file.xml"));

//get all elements called fo:external-graphic
NodeList list = doc.getElementsByTagName("fo:external-graphic");
for(int i = 0 ; i < list.getLength() ; i++){
    Element element = (Element)list.item(i);
    NamedNodeMap attributes = element.getAttributes();

    //change the src attribute
    Attr src = (Attr)attributes.getNamedItem("src");
    System.out.println(src.getValue());
    src.setValue("url('another/path/box.jpg')");
}

//let's print out the resulting doc for debugging purposes
OutputFormat format = new OutputFormat(doc);
format.setLineWidth(65);
format.setIndenting(true);
format.setIndent(2);
Writer out = new StringWriter();
XMLSerializer serializer = new XMLSerializer(out, format);
serializer.serialize(doc);
System.out.println(out.toString());