我希望能够在给定一组XPath映射的情况下生成完整的XML文件.
输入可以在两个映射中指定:(1)一个列出XPath表达式和值; (2)定义适当名称空间的另一个.
/create/article[1]/id => 1
/create/article[1]/description => bar
/create/article[1]/name[1] => foo
/create/article[1]/price[1]/amount => 00.00
/create/article[1]/price[1]/currency => USD
/create/article[2]/id => 2
/create/article[2]/description => some name
/create/article[2]/name[1] => some description
/create/article[2]/price[1]/amount => 00.01
/create/article[2]/price[1]/currency => USD
Run Code Online (Sandbox Code Playgroud)
对于名称空间:
/create => xmlns:ns1='http://predic8.com/wsdl/material/ArticleService/1/
/create/article => xmlns:ns1='http://predic8.com/material/1/‘
/create/article/price => xmlns:ns1='http://predic8.com/common/1/‘
/create/article/id => xmlns:ns1='http://predic8.com/material/1/'
Run Code Online (Sandbox Code Playgroud)
另请注意,我也必须处理XPath 属性表达式.例如:我还应该能够处理属性,例如:
/create/article/@type => richtext
Run Code Online (Sandbox Code Playgroud)
最终输出应该类似于:
<ns1:create xmlns:ns1='http://predic8.com/wsdl/material/ArticleService/1/'>
<ns1:article xmlns:ns1='http://predic8.com/material/1/‘ type='richtext'>
<name>foo</name>
<description>bar</description>
<ns1:price xmlns:ns1='http://predic8.com/common/1/'>
<amount>00.00</amount>
<currency>USD</currency>
</ns1:price>
<ns1:id xmlns:ns1='http://predic8.com/material/1/'>1</ns1:id>
</ns1:article>
<ns1:article xmlns:ns1='http://predic8.com/material/2/‘ type='richtext'>
<name>some name</name>
<description>some description</description>
<ns1:price xmlns:ns1='http://predic8.com/common/2/'>
<amount>00.01</amount> …Run Code Online (Sandbox Code Playgroud) 获取句子中除最后一个单词之外的所有内容的适当正则表达式是什么:
例如:
/hello/world -> /hello/
/one/two/three -> /one/two/
/foo/bar/ice/tea -> /foo/bar/ice/
Run Code Online (Sandbox Code Playgroud) 如何使用 Java 中的 ROME 获取字符串形式的内容以获取某些提要。
\n\n目前这就是我得到的
\n\nString feedURL = \xe2\x80\x9c...\xe2\x80\x9d;\nSyndFeedInput input = new SyndFeedInput();\nSyndFeed feed = input.build(new XmlReader(feedUrl));\n\nSystem.out.println(feed);\n\nfor (final Iterator iter = feed.getModules().iterator(); iter.hasNext();){\n System.out.println("\\t" + ((Module)iter.next()).getUri());\n}\n\nSystem.out.println("Titles of the " + feed.getEntries().size() + " entries:");\nfor (final Iterator iter = feed.getEntries().iterator(); iter.hasNext();){\n System.out.println("\\t" + ((SyndEntry)iter.next()).getContents());\n}\nRun Code Online (Sandbox Code Playgroud)\n\n那么这个的输出是:
\n\nSyndContentImpl.interface=interface com.sun.syndication.feed.synd.SyndContent\nSyndContentImpl.type=html\nSyndContentImpl.mode=null\n\n SyndContentImpl.value= <p><a href="http://www.flickr.com/people/64539367@N07/">MiscDot</a> posted a photo:</p>\n\n<p><a href="http://www.flickr.com/photos/64539367@N07/5954881384/" title="03a"><img src="http://farm7.static.flickr.com/6024/5954881384_5390838321_m.jpg" width="240" height="240" alt="03a" /></a></p>\nRun Code Online (Sandbox Code Playgroud)\n\n但我想要的只是获取内容的字符串:\nSyndContentsImpl.value
在我的程序中,我在池中有几个线程,每个线程都尝试写入数据库.创建的线程数是动态的.当创建的线程数只有一个时,一切正常.但是,当有多线程执行时,我收到错误:
org.apache.ddlutils.DatabaseOperationException: org.postgresql.util.PSQLException: Cannot commit when autoCommit is enabled.
我猜,也许因为每个线程并行执行,两个线程正在尝试同时写入并给出此错误.
您是否认为是这种情况,如果不是,可能导致此错误的原因是什么?
否则,如果我说的是问题,我可以做些什么来解决它?
有人可以告诉我在从某些Http调用获得响应时解压缩GZIP内容需要做些什么.
要拨打电话我使用Jersey客户端API,请参阅以下代码:
String baseURI = "http://api.stackoverflow.com/1.1/answers/7539863?body=true&comments=false";
ClientConfig config = new DefaultClientConfig();
Client client = Client.create(config);
WebResource wr = client.resource(baseURI);
ClientResponse response = null;
response = wr.get(ClientResponse.class);
String response_data = response.getEntity(String.class);
System.out.println(response_data);
Run Code Online (Sandbox Code Playgroud)
但输出是GZIP,看起来像:
{J?J??t??`$?@??????....
Run Code Online (Sandbox Code Playgroud)
如果我能实现以下内容会很好:
假设我有以下示例 XML 文件:
\n\n<ns1:create xmlns:ns1='http://predic8.com/wsdl/material/ArticleService/1/'>\n <article xmlns:ns1='http://predic8.com/material/1/'>\n <name xmlns:ns1='http://predic8.com/material/1/'>foo</name>\n <description xmlns:ns1='http://predic8.com/material/1/'>bar</description>\n <price xmlns:ns1='http://predic8.com/common/1/'>\n <amount xmlns:ns1='http://predic8.com/common/1/'>00.00</amount>\n <currency xmlns:ns1='http://predic8.com/common/1/'>USD</currency>\n </price>\n <id xmlns:ns1='http://predic8.com/material/1/'>1</id>\n </article>\n</ns1:create>\nRun Code Online (Sandbox Code Playgroud)\n\n将其扁平化为一组 xpath 表达式的最佳(最有效)方法是什么。\n另请注意:我想忽略任何命名空间和属性信息。(如果需要,这也可以作为预处理步骤来完成)。
\n\n所以我想得到作为输出:
\n\n/create/article/name\n/create/article/description\n/create/article/price/amount\n/create/article/price/currency\n/create/article/id\nRun Code Online (Sandbox Code Playgroud)\n\nI\xe2\x80\x99m 用 Java 实现。
\n\n编辑: \nPS,在文本节点没有数据的情况下,我可能还需要它工作,因此例如,以下内容应生成与上面相同的输出:
\n\n<ns1:create xmlns:ns1='http://predic8.com/wsdl/material/ArticleService/1/'>\n <article xmlns:ns1='http://predic8.com/material/1/'>\n <name />\n <description />\n <price xmlns:ns1='http://predic8.com/common/1/'>\n <amount />\n <currency xmlns:ns1='http://predic8.com/common/1/'></currency>\n </price>\n <id xmlns:ns1='http://predic8.com/material/1/'></id>\n </article>\n</ns1:create>\nRun Code Online (Sandbox Code Playgroud)\n 有人可以解释为什么会发生这种情况:
class Apple {
String type;
setType(){
System.out.println("inside apple class");
this.type = "apple";
}
}
class RedApple extends Apple {
String type;
setType() {
System.out.println("inside red-apple class");
this.type = "redapple";
}
}
int main {
RedApple red = new RedApple();
Apple apple = (Apple) red;
apple.setType();
}
Run Code Online (Sandbox Code Playgroud)
但产生的输出是:
"inside red-apple class”
Run Code Online (Sandbox Code Playgroud)
为什么该.setType()方法执行子类方法,而不是超类方法,即使我正在进行上传,可以看出?
我希望能够从给定的XML文件中获取所有命名空间信息.
例如,如果输入XML文件类似于:
<ns1:create xmlns:ns1="http://predic8.com/wsdl/material/ArticleService/1/">
<ns1:article xmlns:ns1="xmlns:ns1='http://predic8.com/material/1/">
<ns1:id>1</ns1:id>
<description>bar</description>
<name>foo</name>
<ns1:price>
<amount>00.00</amount>
<currency>USD</currency>
</ns1:price>
<ns1:price>
<amount>11.11</amount>
<currency>AUD</currency>
</ns1:price>
</ns1:article>
<ns1:article xmlns:ns1="xmlns:ns1='http://predic8.com/material/1/">
<ns1:id>2</ns1:id>
<description>some name</description>
<name>some description</name>
<ns1:price>
<amount>00.01</amount>
<currency>USD</currency>
</ns1:price>
</ns1:article>
</ns1:create>
Run Code Online (Sandbox Code Playgroud)
我希望输出看起来像这样(在这种情况下以逗号分隔):
create, ns1, http://predic8.com/wsdl/material/ArticleService/1/
article, ns1, http://predic8.com/material/1/
price, ns1, http://predic8.com/material/1/
id, ns1, http://predic8.com/material/1/
Run Code Online (Sandbox Code Playgroud)
重要笔记:
重要的是我们还要考虑在特定命名空间内定义的子节点,但其定义可以在更高节点处定义.例如,我们仍然想要获取节点ns1:id,我们需要追溯到父节点ns1:article以发现命名空间URL是xmlns:ns1='http://predic8.com/material/1/
我在Java中实现,所以我不介意基于Java的解决方案,甚至基于XSLT的解决方案似乎都是合适的.
有人可以帮我理解在ATOM提要中使用"链接"标签的内容吗?
任何有关这方面的信息将非常感谢!
我在下面提供了一个示例代码段.
<?xml version="1.0"?>
<atom:feed>
<link rel="self" href="http://publisher.example.com/happycats.xml" />
<updated>2008-08-11T02:15:01Z</updated>
<!-- Example of a full entry. -->
<entry>
<title>Heathcliff</title>
<link href="http://publisher.example.com/happycat25.xml" />
<id>http://publisher.example.com/happycat25.xml</id>
<updated>2008-08-11T02:15:01Z</updated>
<content>
What a happy cat. Full content goes here.
</content>
</entry>
Run Code Online (Sandbox Code Playgroud) 按照评论编辑
出于某种原因,当我尝试在Java中解析和格式化时,我似乎正在随机发生此异常.DateString
这是我得到的代码片段(这仅用于测试目的):
SimpleDateFormat formatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
for(...){
...
System.out.println("Before parsing: [" + lastEntryDate + "]");
Date date = formatter.parse(lastEntryDate);
System.out.println("After parsing: [" + date + "]”);
}
Run Code Online (Sandbox Code Playgroud)
并输出:
Before parsing: [Sun Aug 07 22:45:30 EST 2011]
After parsing: [Sun Aug 07 22:45:30 EST 2011]
Before parsing: [Sun Aug 07 22:45:31 EST 2011]
After parsing: [Sun Aug 07 22:45:31 EST 2011]
Before parsing: [Sun Aug 07 22:45:31 EST 2011]
After …Run Code Online (Sandbox Code Playgroud)